logging output to /var/tmp/build-root/22.03-riscv64/.build.log...


In file included from kpatch_storage.c:28:
include/kpatch_ptrace.h:82:13: error: 'struct user_regs_struct' declared inside parameter list will not be visible outside of 
this definition or declaration [-Werror]
   82 |      struct user_regs_struct *pregs);
      |             ^~~~~~~~~~~~~~~~
include/kpatch_ptrace.h:145:23: error: 'struct user_regs_struct' declared inside parameter list will not be visible outside of 
this definition or declaration [-Werror]
  145 | void copy_regs(struct user_regs_struct *dst,
      |                       ^~~~~~~~~~~~~~~~
include/kpatch_ptrace.h:152:14: error: 'struct user_regs_struct' declared inside parameter list will not be visible outside of 
this definition or declaration [-Werror]
  152 |       struct user_regs_struct *pregs,
      |              ^~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors


分析：出现问题的通常是一个结构体，原因是在其它地方没有发现它的定义。通常是没有包含相应的头文件或者头文件位置不对，或者定义放在了引用之后。

     12 #include <sys/user.h>
     13
     14 #include "list.h"


struct kpatch_process;
typedef struct kpatch_process kpatch_process_t;

struct user_regs_struct
typedef struct user_regs_struct user_regs_struct_t;

感谢您提交，请查收您的优惠码：ADI30。订单满130元立减30元，适用于本商城产品购买，

查看驱动模块中打印信息应该使用什么命令？如何查看内核中已有的字符设备的信息？如何查看正在使用的有哪些中断号？
1) 查看驱动模块中打印信息的命令：dmesg

2) 查看字符设备信息可以用lsmod 和modprobe，lsmod可以查看模块的依赖关系，modprobe在加载模块时会加载其他依赖的模块。

3) 显示当前使用的中断号cat /proc/interrupt

dmesg，可以查看全部消息，这样做比较麻烦。或者，专门在一个终端里面看消息，用root权限执行 cat /proc/kmsg，这个命令不会马上结束，直到你手动ctrl+C为止，kmsg里面就是显示内核消息的，程序中printk的输出都可以看到，这样可以随时看到打印内容


想要查看一些结构体的定义，比如struct ip_mreq，可以查看头文件，所有的系统预定义的结构体都在内核源代码的/include下有定义，
ubuntu里面是/usr/include 。  比如要找上面提到的struct ip_mreq，可以这样：find /usr/include -name "*.h" | xargs grep "struct ip_mreq" -rn ，
结果显示，这个结构体在/usr/include/netinet/in.h 的251行有定义。   其他的情况举一反三即可。 

find /usr/include -name "*.h" | xargs grep "struct user_regs_struct" -rn


osc chroot

find /usr/include -name "*.h" | xargs grep "struct user_regs_struct" -rn

/usr/include/asm/ptrace.h:17: * struct user_regs_struct must form a prefix of struct pt_regs.
/usr/include/asm/ptrace.h:19:struct user_regs_struct {
/usr/include/asm/sigcontext.h:18:       struct user_regs_struct sc_regs;
/usr/include/asm/bpf_perf_event.h:7:typedef struct user_regs_struct bpf_user_pt_regs_t;
/usr/include/asm/elf.h:19:typedef struct user_regs_struct elf_gregset_t;
/usr/include/asm/kvm.h:55:      struct user_regs_struct regs;


https://elixir.bootlin.com/linux/latest/source/arch/riscv/include/uapi/asm/ptrace.h#L19
https://elixir.bootlin.com/linux/latest/source/arch/riscv/include/asm/ptrace.h




src/arch/aarch64/arch_ptrace.c

 grep "struct user_regs_struct" -rn
src/arch/aarch64/arch_ptrace.c:89:                          offsetof(struct user_regs_struct, pc),
src/arch/aarch64/arch_ptrace.c:177:                                 offsetof(struct user_regs_struct,
src/arch/aarch64/arch_ptrace.c:188:                                 offsetof(struct user_regs_struct,
src/arch/aarch64/arch_ptrace.c:206:     struct user_regs_struct regs;
src/arch/aarch64/arch_ptrace.c:232:    struct user_regs_struct regs;
src/arch/aarch64/arch_ptrace.c:240:    memset(&regs, 0, sizeof(struct user_regs_struct));
src/arch/aarch64/arch_ptrace.c:254:                        struct user_regs_struct *pregs,
src/arch/aarch64/arch_ptrace.c:258:     struct user_regs_struct orig_regs, regs;
src/arch/aarch64/arch_ptrace.c:336:void copy_regs(struct user_regs_struct *dst,
src/arch/aarch64/arch_ptrace.c:337:            struct user_regs_struct *src)
src/arch/aarch64/arch_ptrace.c:373:     struct user_regs_struct regs;
src/arch/aarch64/arch_coro.c:37:        struct user_regs_struct regs;
src/arch/x86/arch_ptrace.c:86:                      offsetof(struct user_regs_struct, rip),
src/arch/x86/arch_ptrace.c:174:                             offsetof(struct user_regs_struct,
src/arch/x86/arch_ptrace.c:185:                             offsetof(struct user_regs_struct,
src/arch/x86/arch_ptrace.c:203: struct user_regs_struct regs;
src/arch/x86/arch_ptrace.c:211: memset(&regs, 0, sizeof(struct user_regs_struct));
src/arch/x86/arch_ptrace.c:230: struct user_regs_struct regs;
src/arch/x86/arch_ptrace.c:278: struct user_regs_struct regs;
src/arch/x86/arch_ptrace.c:286: memset(&regs, 0, sizeof(struct user_regs_struct));
src/arch/x86/arch_ptrace.c:300:                    struct user_regs_struct *pregs,
src/arch/x86/arch_ptrace.c:304: struct user_regs_struct orig_regs, regs;
src/arch/x86/arch_ptrace.c:374:void copy_regs(struct user_regs_struct *dst,
src/arch/x86/arch_ptrace.c:375:        struct user_regs_struct *src)
src/arch/x86/arch_ptrace.c:404: struct user_regs_struct regs;
src/arch/x86/arch_coro.c:45:    struct user_regs_struct regs;
src/include/kpatch_ptrace.h:82:                   struct user_regs_struct *pregs);
src/include/kpatch_ptrace.h:145:void copy_regs(struct user_regs_struct *dst,
src/include/kpatch_ptrace.h:146:                      struct user_regs_struct *src);
src/include/kpatch_ptrace.h:152:                           struct user_regs_struct *pregs,
src/kpatch_ptrace.c:480:                      struct user_regs_struct *pregs)
[abuild@openEuler-riscv64 libcareplus-1.0.0]$ less -N src/arch/aarch64/arch_ptrace.c


src/arch/riscv64/arch_ptrace.c



find /usr/include -name "*.h"
/usr/include/thread_db.h
/usr/include/pcre_stringpiece.h
/usr/include/a.out.h
/usr/include/stdlib.h
/usr/include/execinfo.h
/usr/include/ttyent.h
/usr/include/threads.h
/usr/include/gnu/lib-names-lp64d.h
/usr/include/gnu/lib-names.h
/usr/include/gnu/stubs.h
/usr/include/gnu/stubs-lp64d.h
/usr/include/gnu/libc-version.h
/usr/include/locale.h
/usr/include/error.h
/usr/include/stab.h
/usr/include/wait.h
/usr/include/sysexits.h
/usr/include/pty.h
/usr/include/utmpx.h
/usr/include/c++/10.3.1/stdlib.h
/usr/include/c++/10.3.1/parallel/par_loop.h
/usr/include/c++/10.3.1/parallel/algobase.h
/usr/include/c++/10.3.1/parallel/compatibility.h
/usr/include/c++/10.3.1/parallel/equally_split.h
/usr/include/c++/10.3.1/parallel/set_operations.h
/usr/include/c++/10.3.1/parallel/algorithmfwd.h
/usr/include/c++/10.3.1/parallel/tags.h
/usr/include/c++/10.3.1/parallel/algo.h
/usr/include/c++/10.3.1/parallel/random_shuffle.h
/usr/include/c++/10.3.1/parallel/features.h
/usr/include/c++/10.3.1/parallel/partial_sum.h
/usr/include/c++/10.3.1/parallel/basic_iterator.h
/usr/include/c++/10.3.1/parallel/compiletime_settings.h
/usr/include/c++/10.3.1/parallel/for_each.h
/usr/include/c++/10.3.1/parallel/base.h
/usr/include/c++/10.3.1/parallel/find.h
/usr/include/c++/10.3.1/parallel/queue.h
/usr/include/c++/10.3.1/parallel/list_partition.h
/usr/include/c++/10.3.1/parallel/omp_loop.h
/usr/include/c++/10.3.1/parallel/sort.h
/usr/include/c++/10.3.1/parallel/random_number.h
/usr/include/c++/10.3.1/parallel/balanced_quicksort.h
/usr/include/c++/10.3.1/parallel/multiway_merge.h
/usr/include/c++/10.3.1/parallel/iterator.h
/usr/include/c++/10.3.1/parallel/for_each_selectors.h
/usr/include/c++/10.3.1/parallel/settings.h
/usr/include/c++/10.3.1/parallel/merge.h
/usr/include/c++/10.3.1/parallel/quicksort.h
/usr/include/c++/10.3.1/parallel/find_selectors.h
/usr/include/c++/10.3.1/parallel/unique_copy.h
/usr/include/c++/10.3.1/parallel/checkers.h
/usr/include/c++/10.3.1/parallel/numericfwd.h
/usr/include/c++/10.3.1/parallel/partition.h
/usr/include/c++/10.3.1/parallel/multiway_mergesort.h
/usr/include/c++/10.3.1/parallel/types.h
/usr/include/c++/10.3.1/parallel/multiseq_selection.h
/usr/include/c++/10.3.1/parallel/parallel.h
/usr/include/c++/10.3.1/parallel/workstealing.h
/usr/include/c++/10.3.1/parallel/omp_loop_static.h
/usr/include/c++/10.3.1/parallel/search.h
/usr/include/c++/10.3.1/parallel/losertree.h
/usr/include/c++/10.3.1/ext/type_traits.h
/usr/include/c++/10.3.1/ext/stdio_filebuf.h
/usr/include/c++/10.3.1/ext/concurrence.h
/usr/include/c++/10.3.1/ext/pool_allocator.h
/usr/include/c++/10.3.1/ext/bitmap_allocator.h
/usr/include/c++/10.3.1/ext/atomicity.h
/usr/include/c++/10.3.1/ext/enc_filebuf.h
/usr/include/c++/10.3.1/ext/cast.h
/usr/include/c++/10.3.1/ext/throw_allocator.h
/usr/include/c++/10.3.1/ext/pod_char_traits.h
/usr/include/c++/10.3.1/ext/numeric_traits.h
/usr/include/c++/10.3.1/ext/typelist.h
/usr/include/c++/10.3.1/ext/sso_string_base.h
/usr/include/c++/10.3.1/ext/vstring_util.h
/usr/include/c++/10.3.1/ext/codecvt_specializations.h
/usr/include/c++/10.3.1/ext/debug_allocator.h
/usr/include/c++/10.3.1/ext/mt_allocator.h
/usr/include/c++/10.3.1/ext/vstring_fwd.h
/usr/include/c++/10.3.1/ext/alloc_traits.h
/usr/include/c++/10.3.1/ext/stdio_sync_filebuf.h
/usr/include/c++/10.3.1/ext/aligned_buffer.h
/usr/include/c++/10.3.1/ext/pointer.h
/usr/include/c++/10.3.1/ext/extptr_allocator.h
/usr/include/c++/10.3.1/ext/new_allocator.h
/usr/include/c++/10.3.1/ext/malloc_allocator.h
/usr/include/c++/10.3.1/ext/ropeimpl.h
/usr/include/c++/10.3.1/ext/vstring.h
/usr/include/c++/10.3.1/ext/rc_string_base.h
/usr/include/c++/10.3.1/ext/string_conversions.h
/usr/include/c++/10.3.1/pstl/parallel_backend_serial.h
/usr/include/c++/10.3.1/pstl/glue_numeric_impl.h
/usr/include/c++/10.3.1/pstl/glue_memory_impl.h
/usr/include/c++/10.3.1/pstl/pstl_config.h
/usr/include/c++/10.3.1/pstl/numeric_impl.h
/usr/include/c++/10.3.1/pstl/parallel_backend_tbb.h
/usr/include/c++/10.3.1/pstl/execution_impl.h
/usr/include/c++/10.3.1/pstl/algorithm_impl.h
/usr/include/c++/10.3.1/pstl/memory_impl.h
/usr/include/c++/10.3.1/pstl/glue_algorithm_defs.h
/usr/include/c++/10.3.1/pstl/unseq_backend_simd.h
/usr/include/c++/10.3.1/pstl/glue_numeric_defs.h
/usr/include/c++/10.3.1/pstl/utils.h
/usr/include/c++/10.3.1/pstl/parallel_backend.h
/usr/include/c++/10.3.1/pstl/parallel_backend_utils.h
/usr/include/c++/10.3.1/pstl/numeric_fwd.h
/usr/include/c++/10.3.1/pstl/glue_execution_defs.h
/usr/include/c++/10.3.1/pstl/algorithm_fwd.h
/usr/include/c++/10.3.1/pstl/parallel_impl.h
/usr/include/c++/10.3.1/pstl/glue_memory_defs.h
/usr/include/c++/10.3.1/pstl/glue_algorithm_impl.h
/usr/include/c++/10.3.1/pstl/execution_defs.h
/usr/include/c++/10.3.1/tgmath.h
/usr/include/c++/10.3.1/experimental/bits/fs_ops.h
/usr/include/c++/10.3.1/experimental/bits/lfts_config.h
/usr/include/c++/10.3.1/experimental/bits/fs_path.h
/usr/include/c++/10.3.1/experimental/bits/fs_fwd.h
/usr/include/c++/10.3.1/experimental/bits/net.h
/usr/include/c++/10.3.1/experimental/bits/fs_dir.h
/usr/include/c++/10.3.1/experimental/bits/shared_ptr.h
/usr/include/c++/10.3.1/fenv.h
/usr/include/c++/10.3.1/math.h
/usr/include/c++/10.3.1/cxxabi.h
/usr/include/c++/10.3.1/decimal/decimal.h
/usr/include/c++/10.3.1/bits/stl_queue.h
/usr/include/c++/10.3.1/bits/stl_algobase.h
/usr/include/c++/10.3.1/bits/stl_bvector.h
/usr/include/c++/10.3.1/bits/random.h
/usr/include/c++/10.3.1/bits/stl_map.h
/usr/include/c++/10.3.1/bits/ranges_algobase.h
/usr/include/c++/10.3.1/bits/charconv.h
/usr/include/c++/10.3.1/bits/invoke.h
/usr/include/c++/10.3.1/bits/algorithmfwd.h
/usr/include/c++/10.3.1/bits/regex_constants.h
/usr/include/c++/10.3.1/bits/stl_multimap.h
/usr/include/c++/10.3.1/bits/quoted_string.h
/usr/include/c++/10.3.1/bits/stl_iterator.h
/usr/include/c++/10.3.1/bits/shared_ptr_base.h
/usr/include/c++/10.3.1/bits/valarray_after.h
/usr/include/c++/10.3.1/bits/erase_if.h
/usr/include/c++/10.3.1/bits/stl_stack.h
/usr/include/c++/10.3.1/bits/cxxabi_init_exception.h
/usr/include/c++/10.3.1/bits/range_access.h
/usr/include/c++/10.3.1/bits/exception_ptr.h
/usr/include/c++/10.3.1/bits/localefwd.h
/usr/include/c++/10.3.1/bits/uses_allocator.h
/usr/include/c++/10.3.1/bits/shared_ptr_atomic.h
/usr/include/c++/10.3.1/bits/unordered_map.h
/usr/include/c++/10.3.1/bits/fs_ops.h
/usr/include/c++/10.3.1/bits/exception.h
/usr/include/c++/10.3.1/bits/slice_array.h
/usr/include/c++/10.3.1/bits/stl_multiset.h
/usr/include/c++/10.3.1/bits/valarray_before.h
/usr/include/c++/10.3.1/bits/functional_hash.h
/usr/include/c++/10.3.1/bits/parse_numbers.h
/usr/include/c++/10.3.1/bits/memoryfwd.h
/usr/include/c++/10.3.1/bits/stringfwd.h
/usr/include/c++/10.3.1/bits/atomic_lockfree_defines.h
/usr/include/c++/10.3.1/bits/iterator_concepts.h
/usr/include/c++/10.3.1/bits/stl_iterator_base_types.h
/usr/include/c++/10.3.1/bits/concept_check.h
/usr/include/c++/10.3.1/bits/stl_function.h
/usr/include/c++/10.3.1/bits/locale_facets_nonio.h
/usr/include/c++/10.3.1/bits/forward_list.h
/usr/include/c++/10.3.1/bits/unordered_set.h
/usr/include/c++/10.3.1/bits/c++0x_warning.h
/usr/include/c++/10.3.1/bits/fs_path.h
/usr/include/c++/10.3.1/bits/basic_ios.h
/usr/include/c++/10.3.1/bits/stl_relops.h
/usr/include/c++/10.3.1/bits/hashtable_policy.h
/usr/include/c++/10.3.1/bits/allocator.h
/usr/include/c++/10.3.1/bits/std_abs.h
/usr/include/c++/10.3.1/bits/indirect_array.h
/usr/include/c++/10.3.1/bits/unique_ptr.h
/usr/include/c++/10.3.1/bits/specfun.h
/usr/include/c++/10.3.1/bits/ranges_uninitialized.h
/usr/include/c++/10.3.1/bits/stl_set.h
/usr/include/c++/10.3.1/bits/ios_base.h
/usr/include/c++/10.3.1/bits/gslice_array.h
/usr/include/c++/10.3.1/bits/codecvt.h
/usr/include/c++/10.3.1/bits/stl_list.h
/usr/include/c++/10.3.1/bits/exception_defines.h
/usr/include/c++/10.3.1/bits/stl_heap.h
/usr/include/c++/10.3.1/bits/stl_pair.h
/usr/include/c++/10.3.1/bits/ranges_algo.h
/usr/include/c++/10.3.1/bits/uniform_int_dist.h
/usr/include/c++/10.3.1/bits/basic_string.h
/usr/include/c++/10.3.1/bits/stl_iterator_base_funcs.h
/usr/include/c++/10.3.1/bits/cpp_type_traits.h
/usr/include/c++/10.3.1/bits/regex_compiler.h
/usr/include/c++/10.3.1/bits/ostream_insert.h
/usr/include/c++/10.3.1/bits/char_traits.h
/usr/include/c++/10.3.1/bits/fs_fwd.h
/usr/include/c++/10.3.1/bits/atomic_futex.h
/usr/include/c++/10.3.1/bits/stl_tempbuf.h
/usr/include/c++/10.3.1/bits/refwrap.h
/usr/include/c++/10.3.1/bits/range_cmp.h
/usr/include/c++/10.3.1/bits/stl_uninitialized.h
/usr/include/c++/10.3.1/bits/atomic_base.h
/usr/include/c++/10.3.1/bits/hashtable.h
/usr/include/c++/10.3.1/bits/regex_scanner.h
/usr/include/c++/10.3.1/bits/ptr_traits.h
/usr/include/c++/10.3.1/bits/std_function.h
/usr/include/c++/10.3.1/bits/nested_exception.h
/usr/include/c++/10.3.1/bits/locale_classes.h
/usr/include/c++/10.3.1/bits/node_handle.h
/usr/include/c++/10.3.1/bits/mask_array.h
/usr/include/c++/10.3.1/bits/regex_executor.h
/usr/include/c++/10.3.1/bits/move.h
/usr/include/c++/10.3.1/bits/predefined_ops.h
/usr/include/c++/10.3.1/bits/stl_construct.h
/usr/include/c++/10.3.1/bits/alloc_traits.h
/usr/include/c++/10.3.1/bits/regex.h
/usr/include/c++/10.3.1/bits/locale_conv.h
/usr/include/c++/10.3.1/bits/std_mutex.h
/usr/include/c++/10.3.1/bits/regex_automaton.h
/usr/include/c++/10.3.1/bits/postypes.h
/usr/include/c++/10.3.1/bits/functexcept.h
/usr/include/c++/10.3.1/bits/valarray_array.h
/usr/include/c++/10.3.1/bits/cxxabi_forced.h
/usr/include/c++/10.3.1/bits/fs_dir.h
/usr/include/c++/10.3.1/bits/shared_ptr.h
/usr/include/c++/10.3.1/bits/regex_error.h
/usr/include/c++/10.3.1/bits/streambuf_iterator.h
/usr/include/c++/10.3.1/bits/allocated_ptr.h
/usr/include/c++/10.3.1/bits/stl_numeric.h
/usr/include/c++/10.3.1/bits/locale_facets.h
/usr/include/c++/10.3.1/bits/unique_lock.h
/usr/include/c++/10.3.1/bits/enable_special_members.h
/usr/include/c++/10.3.1/bits/stl_tree.h
/usr/include/c++/10.3.1/bits/boost_concept_check.h
/usr/include/c++/10.3.1/bits/stl_raw_storage_iter.h
/usr/include/c++/10.3.1/bits/stl_algo.h
/usr/include/c++/10.3.1/bits/gslice.h
/usr/include/c++/10.3.1/bits/stl_vector.h
/usr/include/c++/10.3.1/bits/hash_bytes.h
/usr/include/c++/10.3.1/bits/stl_deque.h
/usr/include/c++/10.3.1/bits/stream_iterator.h
/usr/include/c++/10.3.1/backward/backward_warning.h
/usr/include/c++/10.3.1/backward/binders.h
/usr/include/c++/10.3.1/backward/hash_fun.h
/usr/include/c++/10.3.1/backward/auto_ptr.h
/usr/include/c++/10.3.1/backward/hashtable.h
/usr/include/c++/10.3.1/complex.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/ext/opt_random.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/stdtr1c++.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/gthr-default.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/cpu_defines.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/c++allocator.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/c++io.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/gthr-single.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/c++config.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/gthr.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/cxxabi_tweaks.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/os_defines.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/error_constants.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/time_members.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/ctype_inline.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/gthr-posix.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/basic_file.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/opt_random.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/messages_members.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/stdc++.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/c++locale.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/atomic_word.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/extc++.h
/usr/include/c++/10.3.1/riscv64-linux-gnu/bits/ctype_base.h
/usr/include/c++/10.3.1/debug/safe_sequence.h
/usr/include/c++/10.3.1/debug/stl_iterator.h
/usr/include/c++/10.3.1/debug/map.h
/usr/include/c++/10.3.1/debug/safe_container.h
/usr/include/c++/10.3.1/debug/functions.h
/usr/include/c++/10.3.1/debug/set.h
/usr/include/c++/10.3.1/debug/multiset.h
/usr/include/c++/10.3.1/debug/multimap.h
/usr/include/c++/10.3.1/debug/helper_functions.h
/usr/include/c++/10.3.1/debug/safe_unordered_container.h
/usr/include/c++/10.3.1/debug/safe_unordered_base.h
/usr/include/c++/10.3.1/debug/safe_local_iterator.h
/usr/include/c++/10.3.1/debug/formatter.h
/usr/include/c++/10.3.1/debug/safe_iterator.h
/usr/include/c++/10.3.1/debug/macros.h
/usr/include/c++/10.3.1/debug/safe_base.h
/usr/include/c++/10.3.1/debug/assertions.h
/usr/include/c++/10.3.1/debug/debug.h
/usr/include/c++/10.3.1/tr1/stdlib.h
/usr/include/c++/10.3.1/tr1/random.h
/usr/include/c++/10.3.1/tr1/unordered_map.h
/usr/include/c++/10.3.1/tr1/tgmath.h
/usr/include/c++/10.3.1/tr1/stdio.h
/usr/include/c++/10.3.1/tr1/functional_hash.h
/usr/include/c++/10.3.1/tr1/unordered_set.h
/usr/include/c++/10.3.1/tr1/fenv.h
/usr/include/c++/10.3.1/tr1/wctype.h
/usr/include/c++/10.3.1/tr1/hashtable_policy.h
/usr/include/c++/10.3.1/tr1/math.h
/usr/include/c++/10.3.1/tr1/stdarg.h
/usr/include/c++/10.3.1/tr1/float.h
/usr/include/c++/10.3.1/tr1/ctype.h
/usr/include/c++/10.3.1/tr1/complex.h
/usr/include/c++/10.3.1/tr1/inttypes.h
/usr/include/c++/10.3.1/tr1/stdbool.h
/usr/include/c++/10.3.1/tr1/stdint.h
/usr/include/c++/10.3.1/tr1/hashtable.h
/usr/include/c++/10.3.1/tr1/wchar.h
/usr/include/c++/10.3.1/tr1/limits.h
/usr/include/c++/10.3.1/tr1/shared_ptr.h
/usr/include/c++/10.3.1/tr1/special_function_util.h
/usr/include/utime.h
/usr/include/utmp.h
/usr/include/fnmatch.h
/usr/include/finclude/math-vector-fortran.h
/usr/include/time.h
/usr/include/syscall.h
/usr/include/lzma.h
/usr/include/regexp.h
/usr/include/netrom/netrom.h
/usr/include/err.h
/usr/include/signal.h
/usr/include/byteswap.h
/usr/include/tgmath.h
/usr/include/string.h
/usr/include/features.h
/usr/include/stdio.h
/usr/include/zlib.h
/usr/include/zconf.h
/usr/include/crypt.h
/usr/include/pcreposix.h
/usr/include/stdc-predef.h
/usr/include/gdb/jit-reader.h
/usr/include/errno.h
/usr/include/langinfo.h
/usr/include/rpc/netdb.h
/usr/include/ucontext.h
/usr/include/sched.h
/usr/include/fts.h
/usr/include/fpu_control.h
/usr/include/gnumake.h
/usr/include/iconv.h
/usr/include/pcrecpparg.h
/usr/include/protocols/routed.h
/usr/include/protocols/talkd.h
/usr/include/protocols/timed.h
/usr/include/protocols/rwhod.h
/usr/include/fenv.h
/usr/include/lastlog.h
/usr/include/resolv.h
/usr/include/netatalk/at.h
/usr/include/ar.h
/usr/include/monetary.h
/usr/include/cpio.h
/usr/include/aio.h
/usr/include/aliases.h
/usr/include/wctype.h
/usr/include/paths.h
/usr/include/semaphore.h
/usr/include/glob.h
/usr/include/libintl.h
/usr/include/math.h
/usr/include/ieee754.h
/usr/include/spawn.h
/usr/include/wordexp.h
/usr/include/ftw.h
/usr/include/argp.h
/usr/include/syslog.h
/usr/include/netax25/ax25.h
/usr/include/features-time64.h
/usr/include/fstab.h
/usr/include/shadow.h
/usr/include/envz.h
/usr/include/gconv.h
/usr/include/bits/termios-misc.h
/usr/include/bits/a.out.h
/usr/include/bits/stdlib.h
/usr/include/bits/sigaction.h
/usr/include/bits/sem.h
/usr/include/bits/stat.h
/usr/include/bits/strings_fortified.h
/usr/include/bits/wchar-ldbl.h
/usr/include/bits/stdint-intn.h
/usr/include/bits/waitflags.h
/usr/include/bits/libm-simd-decl-stubs.h
/usr/include/bits/syslog-path.h
/usr/include/bits/utsname.h
/usr/include/bits/statvfs.h
/usr/include/bits/getopt_core.h
/usr/include/bits/locale.h
/usr/include/bits/error.h
/usr/include/bits/sigstack.h
/usr/include/bits/procfs-extra.h
/usr/include/bits/monetary-ldbl.h
/usr/include/bits/iscanonical.h
/usr/include/bits/utmpx.h
/usr/include/bits/utmp.h
/usr/include/bits/sigcontext.h
/usr/include/bits/termios-tcflow.h
/usr/include/bits/unistd_ext.h
/usr/include/bits/time.h
/usr/include/bits/resource.h
/usr/include/bits/syscall.h
/usr/include/bits/uio-ext.h
/usr/include/bits/pthread_stack_min.h
/usr/include/bits/mathcalls-narrow.h
/usr/include/bits/param.h
/usr/include/bits/stdint-uintn.h
/usr/include/bits/statx-generic.h
/usr/include/bits/error-ldbl.h
/usr/include/bits/shmlba.h
/usr/include/bits/byteswap.h
/usr/include/bits/getopt_ext.h
/usr/include/bits/types/__fpos_t.h
/usr/include/bits/types/wint_t.h
/usr/include/bits/types/struct_msqid_ds.h
/usr/include/bits/types/res_state.h
/usr/include/bits/types/siginfo_t.h
/usr/include/bits/types/struct_timeval.h
/usr/include/bits/types/struct_FILE.h
/usr/include/bits/types/FILE.h
/usr/include/bits/types/cookie_io_functions_t.h
/usr/include/bits/types/__mbstate_t.h
/usr/include/bits/types/struct_osockaddr.h
/usr/include/bits/types/sigevent_t.h
/usr/include/bits/types/mbstate_t.h
/usr/include/bits/types/__sigval_t.h
/usr/include/bits/types/struct_tm.h
/usr/include/bits/types/struct_msqid64_ds.h
/usr/include/bits/types/struct___jmp_buf_tag.h
/usr/include/bits/types/struct_msqid64_ds_helper.h
/usr/include/bits/types/struct_semid64_ds.h
/usr/include/bits/types/struct_semid64_ds_helper.h
/usr/include/bits/types/timer_t.h
/usr/include/bits/types/struct_semid_ds.h
/usr/include/bits/types/__sigset_t.h
/usr/include/bits/types/sig_atomic_t.h
/usr/include/bits/types/struct_statx.h
/usr/include/bits/types/struct_rusage.h
/usr/include/bits/types/struct_sigstack.h
/usr/include/bits/types/sigval_t.h
/usr/include/bits/types/clock_t.h
/usr/include/bits/types/time_t.h
/usr/include/bits/types/__locale_t.h
/usr/include/bits/types/__fpos64_t.h
/usr/include/bits/types/sigset_t.h
/usr/include/bits/types/struct_iovec.h
/usr/include/bits/types/struct_shmid_ds.h
/usr/include/bits/types/struct_shmid64_ds.h
/usr/include/bits/types/locale_t.h
/usr/include/bits/types/struct_timespec.h
/usr/include/bits/types/struct_itimerspec.h
/usr/include/bits/types/clockid_t.h
/usr/include/bits/types/stack_t.h
/usr/include/bits/types/__FILE.h
/usr/include/bits/types/struct_statx_timestamp.h
/usr/include/bits/types/struct_shmid64_ds_helper.h
/usr/include/bits/types/error_t.h
/usr/include/bits/types/struct_sched_param.h
/usr/include/bits/types/struct_timeb.h
/usr/include/bits/stdio2.h
/usr/include/bits/libc-header-start.h
/usr/include/bits/fcntl-linux.h
/usr/include/bits/stdio.h
/usr/include/bits/sysmacros.h
/usr/include/bits/ss_flags.h
/usr/include/bits/environments.h
/usr/include/bits/procfs-id.h
/usr/include/bits/statx.h
/usr/include/bits/termios-baud.h
/usr/include/bits/math-vector.h
/usr/include/bits/ipc.h
/usr/include/bits/siginfo-consts-arch.h
/usr/include/bits/shm.h
/usr/include/bits/pthreadtypes-arch.h
/usr/include/bits/ptrace-shared.h
/usr/include/bits/errno.h
/usr/include/bits/sigstksz.h
/usr/include/bits/ipctypes.h
/usr/include/bits/struct_stat_time64_helper.h
/usr/include/bits/sched.h
/usr/include/bits/endianness.h
/usr/include/bits/uio_lim.h
/usr/include/bits/inotify.h
/usr/include/bits/cmathcalls.h
/usr/include/bits/fenv.h
/usr/include/bits/elfclass.h
/usr/include/bits/getopt_posix.h
/usr/include/bits/stdio-ldbl.h
/usr/include/bits/mman-map-flags-generic.h
/usr/include/bits/rseq.h
/usr/include/bits/siginfo-consts.h
/usr/include/bits/stdlib-bsearch.h
/usr/include/bits/select.h
/usr/include/bits/socket-constants.h
/usr/include/bits/string_fortified.h
/usr/include/bits/termios-c_cflag.h
/usr/include/bits/termios-struct.h
/usr/include/bits/mathdef.h
/usr/include/bits/semaphore.h
/usr/include/bits/msq.h
/usr/include/bits/in.h
/usr/include/bits/timex.h
/usr/include/bits/mman-linux.h
/usr/include/bits/syslog.h
/usr/include/bits/siginfo-arch.h
/usr/include/bits/setjmp2.h
/usr/include/bits/stdio_lim.h
/usr/include/bits/select2.h
/usr/include/bits/floatn.h
/usr/include/bits/struct_stat.h
/usr/include/bits/stdlib-float.h
/usr/include/bits/typesizes.h
/usr/include/bits/termios-c_lflag.h
/usr/include/bits/posix_opt.h
/usr/include/bits/timerfd.h
/usr/include/bits/confname.h
/usr/include/bits/procfs-prregset.h
/usr/include/bits/mathcalls.h
/usr/include/bits/poll.h
/usr/include/bits/procfs.h
/usr/include/bits/socket2.h
/usr/include/bits/statfs.h
/usr/include/bits/cpu-set.h
/usr/include/bits/netdb.h
/usr/include/bits/fp-fast.h
/usr/include/bits/initspin.h
/usr/include/bits/socket_type.h
/usr/include/bits/posix2_lim.h
/usr/include/bits/mman-shared.h
/usr/include/bits/termios-c_cc.h
/usr/include/bits/dirent.h
/usr/include/bits/stdlib-ldbl.h
/usr/include/bits/ioctl-types.h
/usr/include/bits/struct_rwlock.h
/usr/include/bits/fp-logb.h
/usr/include/bits/wchar.h
/usr/include/bits/floatn-common.h
/usr/include/bits/pthreadtypes.h
/usr/include/bits/flt-eval-method.h
/usr/include/bits/struct_mutex.h
/usr/include/bits/signal_ext.h
/usr/include/bits/argp-ldbl.h
/usr/include/bits/signum-generic.h
/usr/include/bits/fcntl2.h
/usr/include/bits/sockaddr.h
/usr/include/bits/printf-ldbl.h
/usr/include/bits/sigthread.h
/usr/include/bits/link.h
/usr/include/bits/endian.h
/usr/include/bits/syslog-ldbl.h
/usr/include/bits/mman.h
/usr/include/bits/waitstatus.h
/usr/include/bits/hwcap.h
/usr/include/bits/wctype-wchar.h
/usr/include/bits/wordsize.h
/usr/include/bits/fcntl.h
/usr/include/bits/termios-c_oflag.h
/usr/include/bits/eventfd.h
/usr/include/bits/uintn-identity.h
/usr/include/bits/xopen_lim.h
/usr/include/bits/time64.h
/usr/include/bits/mqueue2.h
/usr/include/bits/ioctls.h
/usr/include/bits/socket.h
/usr/include/bits/mqueue.h
/usr/include/bits/timesize.h
/usr/include/bits/poll2.h
/usr/include/bits/signalfd.h
/usr/include/bits/termios-c_iflag.h
/usr/include/bits/pthread_stack_min-dynamic.h
/usr/include/bits/err-ldbl.h
/usr/include/bits/epoll.h
/usr/include/bits/unistd.h
/usr/include/bits/ipc-perm.h
/usr/include/bits/types.h
/usr/include/bits/sigevent-consts.h
/usr/include/bits/termios.h
/usr/include/bits/setjmp.h
/usr/include/bits/posix1_lim.h
/usr/include/bits/wchar2.h
/usr/include/bits/mathcalls-helper-functions.h
/usr/include/bits/thread-shared-types.h
/usr/include/bits/signum-arch.h
/usr/include/bits/indirect-return.h
/usr/include/bits/local_lim.h
/usr/include/bits/dirent_ext.h
/usr/include/bits/dlfcn.h
/usr/include/bits/long-double.h
/usr/include/grp.h
/usr/include/pcre.h
/usr/include/ctype.h
/usr/include/netash/ash.h
/usr/include/poll.h
/usr/include/scsi/sg.h
/usr/include/scsi/scsi.h
/usr/include/scsi/scsi_ioctl.h
/usr/include/net/ppp_defs.h
/usr/include/net/route.h
/usr/include/net/if_slip.h
/usr/include/net/if_ppp.h
/usr/include/net/if_arp.h
/usr/include/net/if_packet.h
/usr/include/net/ppp-comp.h
/usr/include/net/ethernet.h
/usr/include/net/if.h
/usr/include/net/if_shaper.h
/usr/include/printf.h
/usr/include/elf.h
/usr/include/stdio_ext.h
/usr/include/netdb.h
/usr/include/complex.h
/usr/include/inttypes.h
/usr/include/dirent.h
/usr/include/proc_service.h
/usr/include/ifaddrs.h
/usr/include/values.h
/usr/include/stdint.h
/usr/include/nl_types.h
/usr/include/argz.h
/usr/include/gnu-versions.h
/usr/include/xcrypt.h
/usr/include/lzma/hardware.h
/usr/include/lzma/delta.h
/usr/include/lzma/index.h
/usr/include/lzma/bcj.h
/usr/include/lzma/container.h
/usr/include/lzma/vli.h
/usr/include/lzma/base.h
/usr/include/lzma/version.h
/usr/include/lzma/lzma12.h
/usr/include/lzma/block.h
/usr/include/lzma/filter.h
/usr/include/lzma/stream_flags.h
/usr/include/lzma/index_hash.h
/usr/include/lzma/check.h
/usr/include/tar.h
/usr/include/sys/single_threaded.h
/usr/include/sys/gmon.h
/usr/include/sys/ioctl.h
/usr/include/sys/ptrace.h
/usr/include/sys/un.h
/usr/include/sys/klog.h
/usr/include/sys/sem.h
/usr/include/sys/stat.h
/usr/include/sys/random.h
/usr/include/sys/utsname.h
/usr/include/sys/statvfs.h
/usr/include/sys/wait.h
/usr/include/sys/auxv.h
/usr/include/sys/mount.h
/usr/include/sys/time.h
/usr/include/sys/resource.h
/usr/include/sys/syscall.h
/usr/include/sys/vt.h
/usr/include/sys/sysinfo.h
/usr/include/sys/param.h
/usr/include/sys/socketvar.h
/usr/include/sys/signal.h
/usr/include/sys/fsuid.h
/usr/include/sys/sysmacros.h
/usr/include/sys/sdt-config.h
/usr/include/sys/vfs.h
/usr/include/sys/kd.h
/usr/include/sys/ipc.h
/usr/include/sys/shm.h
/usr/include/sys/msg.h
/usr/include/sys/errno.h
/usr/include/sys/times.h
/usr/include/sys/ucontext.h
/usr/include/sys/inotify.h
/usr/include/sys/soundcard.h
/usr/include/sys/rseq.h
/usr/include/sys/asm.h
/usr/include/sys/pci.h
/usr/include/sys/select.h
/usr/include/sys/bitypes.h
/usr/include/sys/queue.h
/usr/include/sys/profil.h
/usr/include/sys/timex.h
/usr/include/sys/syslog.h
/usr/include/sys/sdt.h
/usr/include/sys/sendfile.h
/usr/include/sys/reboot.h
/usr/include/sys/xattr.h
/usr/include/sys/cachectl.h
/usr/include/sys/prctl.h
/usr/include/sys/gmon_out.h
/usr/include/sys/timerfd.h
/usr/include/sys/file.h
/usr/include/sys/poll.h
/usr/include/sys/procfs.h
/usr/include/sys/statfs.h
/usr/include/sys/quota.h
/usr/include/sys/uio.h
/usr/include/sys/fanotify.h
/usr/include/sys/acct.h
/usr/include/sys/timeb.h
/usr/include/sys/vlimit.h
/usr/include/sys/mman.h
/usr/include/sys/mtio.h
/usr/include/sys/ttychars.h
/usr/include/sys/fcntl.h
/usr/include/sys/eventfd.h
/usr/include/sys/socket.h
/usr/include/sys/personality.h
/usr/include/sys/signalfd.h
/usr/include/sys/swap.h
/usr/include/sys/ttydefaults.h
/usr/include/sys/cdefs.h
/usr/include/sys/epoll.h
/usr/include/sys/unistd.h
/usr/include/sys/types.h
/usr/include/sys/termios.h
/usr/include/sys/dir.h
/usr/include/sys/raw.h
/usr/include/sys/user.h
/usr/include/netiucv/iucv.h
/usr/include/wchar.h
/usr/include/alloca.h
/usr/include/limits.h
/usr/include/pthread.h
/usr/include/sgtty.h
/usr/include/nfs/nfs.h
/usr/include/memory.h
/usr/include/link.h
/usr/include/endian.h
/usr/include/regex.h
/usr/include/netpacket/packet.h
/usr/include/netipx/ipx.h
/usr/include/re_comp.h
/usr/include/fcntl.h
/usr/include/fmtmsg.h
/usr/include/mqueue.h
/usr/include/strings.h
/usr/include/ulimit.h
/usr/include/mcheck.h
/usr/include/obstack.h
/usr/include/pwd.h
/usr/include/libgen.h
/usr/include/unistd.h
/usr/include/mntent.h
/usr/include/termios.h
/usr/include/setjmp.h
/usr/include/netrose/rose.h
/usr/include/uchar.h
/usr/include/netinet/ip.h
/usr/include/netinet/ip6.h
/usr/include/netinet/if_ether.h
/usr/include/netinet/tcp.h
/usr/include/netinet/ether.h
/usr/include/netinet/in_systm.h
/usr/include/netinet/if_fddi.h
/usr/include/netinet/in.h
/usr/include/netinet/udp.h
/usr/include/netinet/icmp6.h
/usr/include/netinet/igmp.h
/usr/include/netinet/ip_icmp.h
/usr/include/netinet/if_tr.h
/usr/include/termio.h
/usr/include/pcre_scanner.h
/usr/include/python3.9/pyconfig-64.h
/usr/include/arpa/tftp.h
/usr/include/arpa/nameser.h
/usr/include/arpa/telnet.h
/usr/include/arpa/nameser_compat.h
/usr/include/arpa/ftp.h
/usr/include/arpa/inet.h
/usr/include/assert.h
/usr/include/malloc.h
/usr/include/search.h
/usr/include/gshadow.h
/usr/include/nss.h
/usr/include/neteconet/ec.h
/usr/include/pcrecpp.h
/usr/include/dlfcn.h
/usr/include/getopt.h

find /usr/include -name "*.h" | xargs grep "_SYS_REG_H" -rn 

subproc_demo.c:16:13: warning: implicit declaration of function ‘fork’ [-Wimplicit-function-declaration]
   16 |     child = fork();
      |             ^~~~
subproc_demo.c:19:7: warning: implicit declaration of function ‘execl’ [-Wimplicit-function-declaration]
   19 |       execl("/bin/ls","ls","-l","-h",NULL);
      |       ^~~~~
subproc_demo.c:19:7: warning: incompatible implicit declaration of built-in function ‘execl’


PTRACE_SYSCALL
find /usr/include -name "*.h" | xargs grep "PTRACE_SYSCALL" -rn 


PTRACE_ATTACH
find /usr/include -name "*.h" | xargs grep "PTRACE_ATTACH" -rn 



简单的说：就是注入到另一个进程里面，外挂用这个函数比较多。

用法：

#include <sys/ptrace.h>
 
long ptrace(enum __ptrace_request request, pid_t pid, void *addr, void *data);

参数：
request：请求执行的行为，可能选择有
PTRACE_TRACEME //指示父进程跟踪某个子进程的执行。任何传给子进程的信号将导致其停止执行，同时父进程调用wait()时会得到通告。之后，子进程调用exec()时，核心会给它传送SIGTRAP信号，在新程序开始执行前，给予父进程控制的机会。pid, addr, 和 data参数被忽略。
以上是唯一由子进程使用的请求，剩下部分将由父进程使用的请求。

PTRACE_PEEKTEXT, PTRACE_PEEKDATA //从子进程内存空间addr指向的位置读取一个字，并作为调用的结果返回。Linux内部对文本段和数据段不加区分，所以目前这两个请求相等。data参数被忽略。
PTRACE_PEEKUSR //从子进程的用户区addr指向的位置读取一个字，并作为调用的结果返回。
PTRACE_POKETEXT, PTRACE_POKEDATA //将data指向的字拷贝到子进程内存空间由addr指向的位置。
PTRACE_POKEUSR //将data指向的字拷贝到子进程用户区由addr指向的位置。
PTRACE_GETREGS, PTRACE_GETFPREGS //将子进程通用和浮点寄存器的值拷贝到父进程内由data指向的位置。addr参数被忽略。
PTRACE_GETSIGINFO //获取导致子进程停止执行的信号信息，并将其存放在父进程内由data指向的位置。addr参数被忽略。
PTRACE_SETREGS, PTRACE_SETFPREGS //从父进程内将data指向的数据拷贝到子进程的通用和浮点寄存器。addr参数被忽略。
PTRACE_SETSIGINFO //将父进程内由data指向的数据作为siginfo_t结构体拷贝到子进程。addr参数被忽略。
PTRACE_SETOPTIONS //将父进程内由data指向的值设定为ptrace选项，data作为位掩码来解释，由下面的标志指定
PTRACE_O_TRACESYSGOOD //当转发syscall陷阱(traps)时，在信号编码中设置位7，即第一个字节的最高位。例如：SIGTRAP | 0x80。这有利于追踪者识别一般的陷阱和那些由syscall引起的陷阱。
PTRACE_O_TRACEFORK //通过 (SIGTRAP | PTRACE_EVENT_FORK << 8) 使子进程下次调用fork()时停止其执行，并自动跟踪开始执行时就已设置SIGSTOP信号的新进程。新进程的PID可以通过PTRACE_GETEVENTMSG获取。
PTRACE_O_TRACEVFORK //通过 (SIGTRAP | PTRACE_EVENT_VFORK << 8) 使子进程下次调用vfork()时停止其执行，并自动跟踪开始执行时就已设置SIGSTOP信号的新进程。新进程的PID可以通过PTRACE_GETEVENTMSG获取。
PTRACE_O_TRACECLONE //通过 (SIGTRAP | PTRACE_EVENT_CLONE << 8) 使子进程下次调用clone()时停止其执行，并自动跟踪开始执行时就已设置SIGSTOP信号的新进程。新进程的PID可以通过PTRACE_GETEVENTMSG获取。
PTRACE_O_TRACEEXEC //通过 (IGTRAP | PTRACE_EVENT_EXEC << 8) 使子进程下次调用exec()时停止其执行。
PTRACE_O_TRACEVFORKDONE //通过 (SIGTRAP | PTRACE_EVENT_VFORK_DONE << 8) 使子进程下次调用exec()并完成时停止其执行。
PTRACE_O_TRACEEXIT //通过 (SIGTRAP | PTRACE_EVENT_EXIT << 8) 使子进程退出时停止其执行。子进程的退出状态可通过PTRACE_GETEVENTMSG。
PTRACE_GETEVENTMSG //获取刚发生的ptrace事件消息，并存放在父进程内由data指向的位置。addr参数被忽略。
PTRACE_CONT //重启动已停止的进程。如果data指向的数据并非0，同时也不是SIGSTOP信号，将会作为传递给子进程的信号来解释。那样，父进程可以控制是否将一个信号发送给子进程。 addr参数被忽略。
PTRACE_SYSCALL, PTRACE_SINGLESTEP //如同PTRACE_CONT一样重启子进程的执行，但指定子进程在下个入口或从系统调用退出时，或者执行单个指令后停止执行，这可用于实现单步调试。addr参数被忽略。
PTRACE_SYSEMU, PTRACE_SYSEMU_SINGLESTEP //用于用户模式的程序仿真子进程的所有系统调用。
PTRACE_KILL //给子进程发送SIGKILL信号，从而终止其执行。data，addr参数被忽略。
PTRACE_ATTACH //衔接到pid指定的进程，从而使其成为当前进程的追踪目标。
PTRACE_DETACH //PTRACE_ATTACH的反向操作。

pid：目标进程标识。
addr：执行 peek 和 poke 操作的目标地址。
data：对于 poke 操作，存放数据的地方。对于 peek 操作，获取数据的地方。

返回说明：
成功执行时，PTRACE_PEEK*请求返回所请求的数据，其它返回0。失败返回-1，errno被设为以下的某个值。由于一个成功的PTRACE_PEEK*请求可能返回-1，决定错误是否发生前，调用者应检查errno。
EBUSY：分配和释放调试寄存器时出错
EFAULT：读写不可访问的内存空间
EINVAL：尝试设置无效选项
EIO：请求无效，或者尝试读写父子进程不可访问的空间
EPERM：没有权限追踪指定的进程
ESRCH：指定的子进程不存在，或者当前正由调用者追踪



long ptrace(enum __ptrace_request request,  pid_t pid, void *addr,  void *data); 

作者：补给站Linux内核 https://www.bilibili.com/read/cv16705682 出处：bilibili

[root@openEuler-riscv64 euler]# ls   /usr/src/kernels/5.17-0.oe2.riscv64/include/asm-generic/
asm-offsets.h        bugs.h           emergency-restart.h  hw_irq.h       kprobes.h       module.h         pgtable-nopmd.h    shmparam.h       tlb.h
asm-prototypes.h     cacheflush.h     error-injection.h    hyperv-tlfs.h  kvm_para.h      module.lds.h     pgtable-nopud.h    signal.h         topology.h
atomic64.h           cache.h          exec.h               ide_iops.h     kvm_types.h     mshyperv.h       pgtable_uffd.h     simd.h           trace_clock.h
atomic.h             checksum.h       export.h             int-ll64.h     linkage.h       msi.h            preempt.h          softirq_stack.h  uaccess.h
audit_change_attr.h  cmpxchg.h        extable.h            ioctl.h        local64.h       nommu_context.h  qrwlock.h          spinlock.h       unaligned.h
audit_dir_write.h    cmpxchg-local.h  fb.h                 io.h           local.h         numa.h           qrwlock_types.h    statfs.h         user.h
audit_read.h         compat.h         fixmap.h             iomap.h        logic_io.h      page.h           qspinlock.h        string.h         vdso
audit_signal.h       current.h        flat.h               irqflags.h     mcs_spinlock.h  param.h          qspinlock_types.h  switch_to.h      vermagic.h
audit_write.h        delay.h          ftrace.h             irq.h          memory_model.h  parport.h        resource.h         syscall.h        vga.h
barrier.h            device.h         futex.h              irq_regs.h     mm_hooks.h      pci.h            rwonce.h           syscalls.h       vmlinux.lds.h
bitops               div64.h          getorder.h           irq_work.h     mmiowb.h        pci_iomap.h      seccomp.h          termios-base.h   vtime.h
bitops.h             dma.h            gpio.h               Kbuild         mmiowb_types.h  percpu.h         sections.h         termios.h        word-at-a-time.h
bitsperlong.h        dma-mapping.h    hardirq.h            kdebug.h       mmu_context.h   pgalloc.h        serial.h           timex.h          xor.h
bug.h                early_ioremap.h  hugetlb.h            kmap_size.h    mmu.h           pgtable-nop4d.h  set_memory.h       tlbflush.h


/include/uapi/linux/ptrace.h   https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/ptrace.h#L11

/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
#ifndef _UAPI_LINUX_PTRACE_H
#define _UAPI_LINUX_PTRACE_H
/* ptrace.h */
/* structs and defines to help the user use the ptrace system call. */

/* has the defines to get at the registers. */

#include <linux/types.h>

#define PTRACE_TRACEME       0
#define PTRACE_PEEKTEXT      1
#define PTRACE_PEEKDATA      2
#define PTRACE_PEEKUSR       3
#define PTRACE_POKETEXT      4
#define PTRACE_POKEDATA      5
#define PTRACE_POKEUSR       6
#define PTRACE_CONT      7
#define PTRACE_KILL      8
#define PTRACE_SINGLESTEP    9

#define PTRACE_ATTACH     16
#define PTRACE_DETACH     17

#define PTRACE_SYSCALL      24

/* 0x4200-0x4300 are reserved for architecture-independent additions.  */
#define PTRACE_SETOPTIONS 0x4200
#define PTRACE_GETEVENTMSG  0x4201
#define PTRACE_GETSIGINFO 0x4202
#define PTRACE_SETSIGINFO 0x4203

/*
 * Generic ptrace interface that exports the architecture specific regsets
 * using the corresponding NT_* types (which are also used in the core dump).
 * Please note that the NT_PRSTATUS note type in a core dump contains a full
 * 'struct elf_prstatus'. But the user_regset for NT_PRSTATUS contains just the
 * elf_gregset_t that is the pr_reg field of 'struct elf_prstatus'. For all the
 * other user_regset flavors, the user_regset layout and the ELF core dump note
 * payload are exactly the same layout.
 *
 * This interface usage is as follows:
 *  struct iovec iov = { buf, len};
 *
 *  ret = ptrace(PTRACE_GETREGSET/PTRACE_SETREGSET, pid, NT_XXX_TYPE, &iov);
 *
 * On the successful completion, iov.len will be updated by the kernel,
 * specifying how much the kernel has written/read to/from the user's iov.buf.
 */
#define PTRACE_GETREGSET  0x4204
#define PTRACE_SETREGSET  0x4205

#define PTRACE_SEIZE    0x4206
#define PTRACE_INTERRUPT  0x4207
#define PTRACE_LISTEN   0x4208

#define PTRACE_PEEKSIGINFO  0x4209

struct ptrace_peeksiginfo_args {
  __u64 off;  /* from which siginfo to start */
  __u32 flags;
  __s32 nr; /* how may siginfos to take */
};

#define PTRACE_GETSIGMASK 0x420a
#define PTRACE_SETSIGMASK 0x420b

#define PTRACE_SECCOMP_GET_FILTER 0x420c
#define PTRACE_SECCOMP_GET_METADATA 0x420d

struct seccomp_metadata {
  __u64 filter_off; /* Input: which filter */
  __u64 flags;    /* Output: filter's flags */
};

#define PTRACE_GET_SYSCALL_INFO   0x420e
#define PTRACE_SYSCALL_INFO_NONE  0
#define PTRACE_SYSCALL_INFO_ENTRY 1
#define PTRACE_SYSCALL_INFO_EXIT  2
#define PTRACE_SYSCALL_INFO_SECCOMP 3

struct ptrace_syscall_info {
  __u8 op;  /* PTRACE_SYSCALL_INFO_* */
  __u8 pad[3];
  __u32 arch;
  __u64 instruction_pointer;
  __u64 stack_pointer;
  union {
    struct {
      __u64 nr;
      __u64 args[6];
    } entry;
    struct {
      __s64 rval;
      __u8 is_error;
    } exit;
    struct {
      __u64 nr;
      __u64 args[6];
      __u32 ret_data;
    } seccomp;
  };
};

#define PTRACE_GET_RSEQ_CONFIGURATION 0x420f

struct ptrace_rseq_configuration {
  __u64 rseq_abi_pointer;
  __u32 rseq_abi_size;
  __u32 signature;
  __u32 flags;
  __u32 pad;
};

/*
 * These values are stored in task->ptrace_message
 * by ptrace_stop to describe the current syscall-stop.
 */
#define PTRACE_EVENTMSG_SYSCALL_ENTRY 1
#define PTRACE_EVENTMSG_SYSCALL_EXIT  2

/* Read signals from a shared (process wide) queue */
#define PTRACE_PEEKSIGINFO_SHARED (1 << 0)

/* Wait extended result codes for the above trace options.  */
#define PTRACE_EVENT_FORK 1
#define PTRACE_EVENT_VFORK  2
#define PTRACE_EVENT_CLONE  3
#define PTRACE_EVENT_EXEC 4
#define PTRACE_EVENT_VFORK_DONE 5
#define PTRACE_EVENT_EXIT 6
#define PTRACE_EVENT_SECCOMP  7
/* Extended result codes which enabled by means other than options.  */
#define PTRACE_EVENT_STOP 128

/* Options set using PTRACE_SETOPTIONS or using PTRACE_SEIZE @data param */
#define PTRACE_O_TRACESYSGOOD 1
#define PTRACE_O_TRACEFORK  (1 << PTRACE_EVENT_FORK)
#define PTRACE_O_TRACEVFORK (1 << PTRACE_EVENT_VFORK)
#define PTRACE_O_TRACECLONE (1 << PTRACE_EVENT_CLONE)
#define PTRACE_O_TRACEEXEC  (1 << PTRACE_EVENT_EXEC)
#define PTRACE_O_TRACEVFORKDONE (1 << PTRACE_EVENT_VFORK_DONE)
#define PTRACE_O_TRACEEXIT  (1 << PTRACE_EVENT_EXIT)
#define PTRACE_O_TRACESECCOMP (1 << PTRACE_EVENT_SECCOMP)

/* eventless options */
#define PTRACE_O_EXITKILL   (1 << 20)
#define PTRACE_O_SUSPEND_SECCOMP  (1 << 21)

#define PTRACE_O_MASK   (\
  0x000000ff | PTRACE_O_EXITKILL | PTRACE_O_SUSPEND_SECCOMP)

#include <asm/ptrace.h>


#endif /* _UAPI_LINUX_PTRACE_H */


find /usr/include/  -name '*.h' |xargs grep -rn 'PTRACE_GETREGS'
/usr/include/linux/ptrace.h:45: *       ret = ptrace(PTRACE_GETREGSET/PTRACE_SETREGSET, pid, NT_XXX_TYPE, &iov);
/usr/include/linux/ptrace.h:50:#define PTRACE_GETREGSET 0x4204
/usr/include/linux/elf.h:369: * using the corresponding note types via the PTRACE_GETREGSET and
/usr/include/sys/ptrace.h:75:  PTRACE_GETREGS = 12,
/usr/include/sys/ptrace.h:76:#define PT_GETREGS PTRACE_GETREGS
/usr/include/sys/ptrace.h:132:  PTRACE_GETREGSET = 0x4204,
/usr/include/sys/ptrace.h:133:#define PTRACE_GETREGSET PTRACE_GETREGSET


---------------
https://sbexr.rabexc.org/latest/sources/b2/ebd3a6a9c58725.html
http://tomoyo.osdn.jp/cgi-bin/lxr/source/arch/x86/include/asm/user_32.h#L77
http://lxr.linux.no/linux-old+v0.95/include/sys/ptrace.h#L44
https://en.opensuse.org/openSUSE:RISC-V

---------------

switch (regs->regs[2]) -------------------switch (regs->a0) 
regs->regs[0] ------------------- regs->orig_a0
regs->cp0_epc ------------------- regs->epc
regs->regs[7] ------------------- regs->a7
regs->regs[26]  ------------------- 



x86
struct user_regs_struct {
    unsigned long r15,r14,r13,r12,rbp,rbx,r11,r10;
    unsigned long r9,r8,rax,rcx,rdx,rsi,rdi,orig_rax;
    unsigned long rip,cs,eflags;
    unsigned long rsp,ss;
    unsigned long fs_base, gs_base;
    unsigned long ds,es,fs,gs;
};

--------------------------------------https://www.cnblogs.com/gavanwanggw/p/6920826.html
linux系统调用表(system call table)
系统调用号 函数名 入口点 源码
0 read  sys_read  fs/read_write.c
1 write sys_write fs/read_write.c
2 open  sys_open  fs/open.c
3 close sys_close fs/open.c
4 stat  sys_newstat fs/stat.c
5 fstat sys_newfstat  fs/stat.c
6 lstat sys_newlstat  fs/stat.c
7 poll  sys_poll  fs/select.c
8 lseek sys_lseek fs/read_write.c
9 mmap  sys_mmap  arch/x86/kernel/sys_x86_64.c
10  mprotect  sys_mprotect  mm/mprotect.c
11  munmap  sys_munmap  mm/mmap.c
12  brk sys_brk mm/mmap.c
13  rt_sigaction  sys_rt_sigaction  kernel/signal.c
14  rt_sigprocmask  sys_rt_sigprocmask  kernel/signal.c
15  rt_sigreturn  stub_rt_sigreturn arch/x86/kernel/signal.c
16  ioctl sys_ioctl fs/ioctl.c
17  pread64 sys_pread64 fs/read_write.c
18  pwrite64  sys_pwrite64  fs/read_write.c
19  readv sys_readv fs/read_write.c
20  writev  sys_writev  fs/read_write.c
21  access  sys_access  fs/open.c
22  pipe  sys_pipe  fs/pipe.c
23  select  sys_select  fs/select.c
24  sched_yield sys_sched_yield kernel/sched/core.c
25  mremap  sys_mremap  mm/mmap.c
26  msync sys_msync mm/msync.c
27  mincore sys_mincore mm/mincore.c
28  madvise sys_madvise mm/madvise.c
29  shmget  sys_shmget  ipc/shm.c
30  shmat sys_shmat ipc/shm.c
31  shmctl  sys_shmctl  ipc/shm.c
32  dup sys_dup fs/file.c
33  dup2  sys_dup2  fs/file.c
34  pause sys_pause kernel/signal.c
35  nanosleep sys_nanosleep kernel/hrtimer.c
36  getitimer sys_getitimer kernel/itimer.c
37  alarm sys_alarm kernel/timer.c
38  setitimer sys_setitimer kernel/itimer.c
39  getpid  sys_getpid  kernel/sys.c
40  sendfile  sys_sendfile64  fs/read_write.c
41  socket  sys_socket  net/socket.c
42  connect sys_connect net/socket.c
43  accept  sys_accept  net/socket.c
44  sendto  sys_sendto  net/socket.c
45  recvfrom  sys_recvfrom  net/socket.c
46  sendmsg sys_sendmsg net/socket.c
47  recvmsg sys_recvmsg net/socket.c
48  shutdown  sys_shutdown  net/socket.c
49  bind  sys_bind  net/socket.c
50  listen  sys_listen  net/socket.c
51  getsockname sys_getsockname net/socket.c
52  getpeername sys_getpeername net/socket.c
53  socketpair  sys_socketpair  net/socket.c
54  setsockopt  sys_setsockopt  net/socket.c
55  getsockopt  sys_getsockopt  net/socket.c
56  clone stub_clone  kernel/fork.c
57  fork  stub_fork kernel/fork.c
58  vfork stub_vfork  kernel/fork.c
59  execve  stub_execve fs/exec.c
60  exit  sys_exit  kernel/exit.c
61  wait4 sys_wait4 kernel/exit.c
62  kill  sys_kill  kernel/signal.c
63  uname sys_newuname  kernel/sys.c
64  semget  sys_semget  ipc/sem.c
65  semop sys_semop ipc/sem.c
66  semctl  sys_semctl  ipc/sem.c
67  shmdt sys_shmdt ipc/shm.c
68  msgget  sys_msgget  ipc/msg.c
69  msgsnd  sys_msgsnd  ipc/msg.c
70  msgrcv  sys_msgrcv  ipc/msg.c
71  msgctl  sys_msgctl  ipc/msg.c
72  fcntl sys_fcntl fs/fcntl.c
73  flock sys_flock fs/locks.c
74  fsync sys_fsync fs/sync.c
75  fdatasync sys_fdatasync fs/sync.c
76  truncate  sys_truncate  fs/open.c
77  ftruncate sys_ftruncate fs/open.c
78  getdents  sys_getdents  fs/readdir.c
79  getcwd  sys_getcwd  fs/dcache.c
80  chdir sys_chdir fs/open.c
81  fchdir  sys_fchdir  fs/open.c
82  rename  sys_rename  fs/namei.c
83  mkdir sys_mkdir fs/namei.c
84  rmdir sys_rmdir fs/namei.c
85  creat sys_creat fs/open.c
86  link  sys_link  fs/namei.c
87  unlink  sys_unlink  fs/namei.c
88  symlink sys_symlink fs/namei.c
89  readlink  sys_readlink  fs/stat.c
90  chmod sys_chmod fs/open.c
91  fchmod  sys_fchmod  fs/open.c
92  chown sys_chown fs/open.c
93  fchown  sys_fchown  fs/open.c
94  lchown  sys_lchown  fs/open.c
95  umask sys_umask kernel/sys.c
96  gettimeofday  sys_gettimeofday  kernel/time.c
97  getrlimit sys_getrlimit kernel/sys.c
98  getrusage sys_getrusage kernel/sys.c
99  sysinfo sys_sysinfo kernel/sys.c
100 times sys_times kernel/sys.c
101 ptrace  sys_ptrace  kernel/ptrace.c
102 getuid  sys_getuid  kernel/sys.c
103 syslog  sys_syslog  kernel/printk/printk.c
104 getgid  sys_getgid  kernel/sys.c
105 setuid  sys_setuid  kernel/sys.c
106 setgid  sys_setgid  kernel/sys.c
107 geteuid sys_geteuid kernel/sys.c
108 getegid sys_getegid kernel/sys.c
109 setpgid sys_setpgid kernel/sys.c
110 getppid sys_getppid kernel/sys.c
111 getpgrp sys_getpgrp kernel/sys.c
112 setsid  sys_setsid  kernel/sys.c
113 setreuid  sys_setreuid  kernel/sys.c
114 setregid  sys_setregid  kernel/sys.c
115 getgroups sys_getgroups kernel/groups.c
116 setgroups sys_setgroups kernel/groups.c
117 setresuid sys_setresuid kernel/sys.c
118 getresuid sys_getresuid kernel/sys.c
119 setresgid sys_setresgid kernel/sys.c
120 getresgid sys_getresgid kernel/sys.c
121 getpgid sys_getpgid kernel/sys.c
122 setfsuid  sys_setfsuid  kernel/sys.c
123 setfsgid  sys_setfsgid  kernel/sys.c
124 getsid  sys_getsid  kernel/sys.c
125 capget  sys_capget  kernel/capability.c
126 capset  sys_capset  kernel/capability.c
127 rt_sigpending sys_rt_sigpending kernel/signal.c
128 rt_sigtimedwait sys_rt_sigtimedwait kernel/signal.c
129 rt_sigqueueinfo sys_rt_sigqueueinfo kernel/signal.c
130 rt_sigsuspend sys_rt_sigsuspend kernel/signal.c
131 sigaltstack sys_sigaltstack kernel/signal.c
132 utime sys_utime fs/utimes.c
133 mknod sys_mknod fs/namei.c
134 uselib    fs/exec.c
135 personality sys_personality kernel/exec_domain.c
136 ustat sys_ustat fs/statfs.c
137 statfs  sys_statfs  fs/statfs.c
138 fstatfs sys_fstatfs fs/statfs.c
139 sysfs sys_sysfs fs/filesystems.c
140 getpriority sys_getpriority kernel/sys.c
141 setpriority sys_setpriority kernel/sys.c
142 sched_setparam  sys_sched_setparam  kernel/sched/core.c
143 sched_getparam  sys_sched_getparam  kernel/sched/core.c
144 sched_setscheduler  sys_sched_setscheduler  kernel/sched/core.c
145 sched_getscheduler  sys_sched_getscheduler  kernel/sched/core.c
146 sched_get_priority_max  sys_sched_get_priority_max  kernel/sched/core.c
147 sched_get_priority_min  sys_sched_get_priority_min  kernel/sched/core.c
148 sched_rr_get_interval sys_sched_rr_get_interval kernel/sched/core.c
149 mlock sys_mlock mm/mlock.c
150 munlock sys_munlock mm/mlock.c
151 mlockall  sys_mlockall  mm/mlock.c
152 munlockall  sys_munlockall  mm/mlock.c
153 vhangup sys_vhangup fs/open.c
154 modify_ldt  sys_modify_ldt  arch/x86/um/ldt.c
155 pivot_root  sys_pivot_root  fs/namespace.c
156 _sysctl sys_sysctl  kernel/sysctl_binary.c
157 prctl sys_prctl kernel/sys.c
158 arch_prctl  sys_arch_prctl  arch/x86/um/syscalls_64.c
159 adjtimex  sys_adjtimex  kernel/time.c
160 setrlimit sys_setrlimit kernel/sys.c
161 chroot  sys_chroot  fs/open.c
162 sync  sys_sync  fs/sync.c
163 acct  sys_acct  kernel/acct.c
164 settimeofday  sys_settimeofday  kernel/time.c
165 mount sys_mount fs/namespace.c
166 umount2 sys_umount  fs/namespace.c
167 swapon  sys_swapon  mm/swapfile.c
168 swapoff sys_swapoff mm/swapfile.c
169 reboot  sys_reboot  kernel/reboot.c
170 sethostname sys_sethostname kernel/sys.c
171 setdomainname sys_setdomainname kernel/sys.c
172 iopl  stub_iopl arch/x86/kernel/ioport.c
173 ioperm  sys_ioperm  arch/x86/kernel/ioport.c
174 create_module   NOT IMPLEMENTED
175 init_module sys_init_module kernel/module.c
176 delete_module sys_delete_module kernel/module.c
177 get_kernel_syms   NOT IMPLEMENTED
178 query_module    NOT IMPLEMENTED
179 quotactl  sys_quotactl  fs/quota/quota.c
180 nfsservctl    NOT IMPLEMENTED
181 getpmsg   NOT IMPLEMENTED
182 putpmsg   NOT IMPLEMENTED
183 afs_syscall   NOT IMPLEMENTED
184 tuxcall   NOT IMPLEMENTED
185 security    NOT IMPLEMENTED
186 gettid  sys_gettid  kernel/sys.c
187 readahead sys_readahead mm/readahead.c
188 setxattr  sys_setxattr  fs/xattr.c
189 lsetxattr sys_lsetxattr fs/xattr.c
190 fsetxattr sys_fsetxattr fs/xattr.c
191 getxattr  sys_getxattr  fs/xattr.c
192 lgetxattr sys_lgetxattr fs/xattr.c
193 fgetxattr sys_fgetxattr fs/xattr.c
194 listxattr sys_listxattr fs/xattr.c
195 llistxattr  sys_llistxattr  fs/xattr.c
196 flistxattr  sys_flistxattr  fs/xattr.c
197 removexattr sys_removexattr fs/xattr.c
198 lremovexattr  sys_lremovexattr  fs/xattr.c
199 fremovexattr  sys_fremovexattr  fs/xattr.c
200 tkill sys_tkill kernel/signal.c
201 time  sys_time  kernel/time.c
202 futex sys_futex kernel/futex.c
203 sched_setaffinity sys_sched_setaffinity kernel/sched/core.c
204 sched_getaffinity sys_sched_getaffinity kernel/sched/core.c
205 set_thread_area   arch/x86/kernel/tls.c
206 io_setup  sys_io_setup  fs/aio.c
207 io_destroy  sys_io_destroy  fs/aio.c
208 io_getevents  sys_io_getevents  fs/aio.c
209 io_submit sys_io_submit fs/aio.c
210 io_cancel sys_io_cancel fs/aio.c
211 get_thread_area   arch/x86/kernel/tls.c
212 lookup_dcookie  sys_lookup_dcookie  fs/dcookies.c
213 epoll_create  sys_epoll_create  fs/eventpoll.c
214 epoll_ctl_old   NOT IMPLEMENTED
215 epoll_wait_old    NOT IMPLEMENTED
216 remap_file_pages  sys_remap_file_pages  mm/fremap.c
217 getdents64  sys_getdents64  fs/readdir.c
218 set_tid_address sys_set_tid_address kernel/fork.c
219 restart_syscall sys_restart_syscall kernel/signal.c
220 semtimedop  sys_semtimedop  ipc/sem.c
221 fadvise64 sys_fadvise64 mm/fadvise.c
222 timer_create  sys_timer_create  kernel/posix-timers.c
223 timer_settime sys_timer_settime kernel/posix-timers.c
224 timer_gettime sys_timer_gettime kernel/posix-timers.c
225 timer_getoverrun  sys_timer_getoverrun  kernel/posix-timers.c
226 timer_delete  sys_timer_delete  kernel/posix-timers.c
227 clock_settime sys_clock_settime kernel/posix-timers.c
228 clock_gettime sys_clock_gettime kernel/posix-timers.c
229 clock_getres  sys_clock_getres  kernel/posix-timers.c
230 clock_nanosleep sys_clock_nanosleep kernel/posix-timers.c
231 exit_group  sys_exit_group  kernel/exit.c
232 epoll_wait  sys_epoll_wait  fs/eventpoll.c
233 epoll_ctl sys_epoll_ctl fs/eventpoll.c
234 tgkill  sys_tgkill  kernel/signal.c
235 utimes  sys_utimes  fs/utimes.c
236 vserver   NOT IMPLEMENTED
237 mbind sys_mbind mm/mempolicy.c
238 set_mempolicy sys_set_mempolicy mm/mempolicy.c
239 get_mempolicy sys_get_mempolicy mm/mempolicy.c
240 mq_open sys_mq_open ipc/mqueue.c
241 mq_unlink sys_mq_unlink ipc/mqueue.c
242 mq_timedsend  sys_mq_timedsend  ipc/mqueue.c
243 mq_timedreceive sys_mq_timedreceive ipc/mqueue.c
244 mq_notify sys_mq_notify ipc/mqueue.c
245 mq_getsetattr sys_mq_getsetattr ipc/mqueue.c
246 kexec_load  sys_kexec_load  kernel/kexec.c
247 waitid  sys_waitid  kernel/exit.c
248 add_key sys_add_key security/keys/keyctl.c
249 request_key sys_request_key security/keys/keyctl.c
250 keyctl  sys_keyctl  security/keys/keyctl.c
251 ioprio_set  sys_ioprio_set  fs/ioprio.c
252 ioprio_get  sys_ioprio_get  fs/ioprio.c
253 inotify_init  sys_inotify_init  fs/notify/inotify/inotify_user.c
254 inotify_add_watch sys_inotify_add_watch fs/notify/inotify/inotify_user.c
255 inotify_rm_watch  sys_inotify_rm_watch  fs/notify/inotify/inotify_user.c
256 migrate_pages sys_migrate_pages mm/mempolicy.c
257 openat  sys_openat  fs/open.c
258 mkdirat sys_mkdirat fs/namei.c
259 mknodat sys_mknodat fs/namei.c
260 fchownat  sys_fchownat  fs/open.c
261 futimesat sys_futimesat fs/utimes.c
262 newfstatat  sys_newfstatat  fs/stat.c
263 unlinkat  sys_unlinkat  fs/namei.c
264 renameat  sys_renameat  fs/namei.c
265 linkat  sys_linkat  fs/namei.c
266 symlinkat sys_symlinkat fs/namei.c
267 readlinkat  sys_readlinkat  fs/stat.c
268 fchmodat  sys_fchmodat  fs/open.c
269 faccessat sys_faccessat fs/open.c
270 pselect6  sys_pselect6  fs/select.c
271 ppoll sys_ppoll fs/select.c
272 unshare sys_unshare kernel/fork.c
273 set_robust_list sys_set_robust_list kernel/futex.c
274 get_robust_list sys_get_robust_list kernel/futex.c
275 splice  sys_splice  fs/splice.c
276 tee sys_tee fs/splice.c
277 sync_file_range sys_sync_file_range fs/sync.c
278 vmsplice  sys_vmsplice  fs/splice.c
279 move_pages  sys_move_pages  mm/migrate.c
280 utimensat sys_utimensat fs/utimes.c
281 epoll_pwait sys_epoll_pwait fs/eventpoll.c
282 signalfd  sys_signalfd  fs/signalfd.c
283 timerfd_create  sys_timerfd_create  fs/timerfd.c
284 eventfd sys_eventfd fs/eventfd.c
285 fallocate sys_fallocate fs/open.c
286 timerfd_settime sys_timerfd_settime fs/timerfd.c
287 timerfd_gettime sys_timerfd_gettime fs/timerfd.c
288 accept4 sys_accept4 net/socket.c
289 signalfd4 sys_signalfd4 fs/signalfd.c
290 eventfd2  sys_eventfd2  fs/eventfd.c
291 epoll_create1 sys_epoll_create1 fs/eventpoll.c
292 dup3  sys_dup3  fs/file.c
293 pipe2 sys_pipe2 fs/pipe.c
294 inotify_init1 sys_inotify_init1 fs/notify/inotify/inotify_user.c
295 preadv  sys_preadv  fs/read_write.c
296 pwritev sys_pwritev fs/read_write.c
297 rt_tgsigqueueinfo sys_rt_tgsigqueueinfo kernel/signal.c
298 perf_event_open sys_perf_event_open kernel/events/core.c
299 recvmmsg  sys_recvmmsg  net/socket.c
300 fanotify_init sys_fanotify_init fs/notify/fanotify/fanotify_user.c
301 fanotify_mark sys_fanotify_mark fs/notify/fanotify/fanotify_user.c
302 prlimit64 sys_prlimit64 kernel/sys.c
303 name_to_handle_at sys_name_to_handle_at fs/fhandle.c
304 open_by_handle_at sys_open_by_handle_at fs/fhandle.c
305 clock_adjtime sys_clock_adjtime kernel/posix-timers.c
306 syncfs  sys_syncfs  fs/sync.c
307 sendmmsg  sys_sendmmsg  net/socket.c
308 setns sys_setns kernel/nsproxy.c
309 getcpu  sys_getcpu  kernel/sys.c
310 process_vm_readv  sys_process_vm_readv  mm/process_vm_access.c
311 process_vm_writev sys_process_vm_writev mm/process_vm_access.c
312 kcmp  sys_kcmp  kernel/kcmp.c
313 finit_module  sys_finit_module  kernel/module.c
-------------------------------------  ^\d+\s+[a-z]+_[a-z]+ 

Tips 1： 用户空间的方法xxx，对应系统调用层方法则是sys_xxx；
Tips 2： unistd.h文件记录着系统调用中断号的信息。
Tips 3： 宏定义SYSCALL_DEFINEx(xxx,…)，展开后对应的方法则是sys_xxx；
Tips 4： 方法参数的个数x，对应于SYSCALL_DEFINEx。
asmlinkage是gcc标签，代表函数读取的参数来自于栈中，而非寄存器。
看到这或许很多人(包括我)会觉得诧异，为何要如此复杂呢，后来查资料，发现这是由于之前64位Linux存在CVE-2009-2009的漏洞，简单说就是32位参数存放在64位寄存器，修改符号扩展可能导致产生一个非法内存地址，从而导致系统崩溃或者提升权限。 为了修复这个问题，把寄存器高位清零即可，但做起来比较困难，为了做尽可能少的修改，将调用参数统一采用使用long型来接收，再强转为相应参数。 窥见一斑，可见Linux大师们精湛的宏定义，已经用得出神入化。
如果觉得很复杂，那么可以忽略这个宏定义，只要记住SYSCALL_DEFINE2(kill, pid_t, pid, int, sig) 基本等价于 asmlinkage long sys_kill(int pid, int sig) 就足够了。
————————————————
版权声明：本文为CSDN博主「暗暗yu」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
原文链接：https://blog.csdn.net/weixin_32267731/article/details/111986480



https://live.bilibili.com/23913668

asm：声明C程序中插入汇编代码，也可写作__asm__。
asm-qualifiers：限定符，其可选值有：
volatile、inline、goto，其中volatile是指示编译器对此汇编代码不进行优化，inline用于指示编译器尽可能小的假设汇编代码指令的大小，goto用于汇编代码可以跳转到一个或多个C标签。
AssemblerTemplate:汇编指令，在使用中需要用双引号包含起来，若存在多条指令，每条指令后需加入\n\t分开。
OutputOperands：输出操作数，内联汇编代码执行时，返回给C程序保存，当存在多个输出参数时，使用逗号分开。格式为：[ [asmSymbolicName] ] constraint (cvariablename)
asmSymbolicName：符号名，任何允许的C变量均是被允许的，同一语句中两个操作数不能使用相同的符号名。
constraint：表示约束，可选值参见表1。
cvariablename：C语言变量名。
InputOperands：输入操作数，从哪里传入操作数据。格式为：[ [asmSymbolicName] ] constraint (cexpression)。
————————————————
版权声明：本文为CSDN博主「Eddy_l」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
原文链接：https://blog.csdn.net/qq_41596356/article/details/125014267
http://hehezhou.cn/gccint/Modifiers.html#Modifiers
https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Clobbers-and-Scratch-Registers
https://www.cnblogs.com/kongj/p/14031770.html
http://www.ethernut.de/en/documents/arm-inline-asm.html


反汇编(Disassembly)：把目标代码转为汇编代码的过程，也可以说是把机器语言转换为汇编语言代码、低级转高级的意思，
常用于软件破解（例如找到它是如何注册的，从而解出它的注册码或者编写注册机）、外挂技术、病毒分析、逆向工程、软件汉化等领域。
学习和理解反汇编语言对软件调试、漏洞分析、OS的内核原理及理解高级语言代码都有相当大的帮助，在此过程中我们可以领悟到软件作者的编程思想。
总之一句话：软件一切神秘的运行机制全在反汇编代码里面。
动态调试工具OD、DEBUG、x64Dbg等静态分析工具IDA Pro、C32Asm等

反汇编一段C语言代码
gcc -S test_compile.c -o test_compile.s -m32
可以回忆一下gcc -E是将C程序预编译为.i文件, -S是编译为汇编语言.s文件，-c是汇编为.o文件，最后还有链接环节变为可执行文件，
这里就是将C语言文件编译为.s汇编文件，-m32是32位编译，需要下载一些库，具体的可以去查。当然编译后会有许多无用的行，即.以后的东西，
我们可以删除它们，留下有用信息。使用如下命令
sed -i '/[.]/d' com_compile.s
或者进入.s文件然后按下ESC，：输入g/.s*/d。最后剩下的汇编语句如下(Linux汇编为AT&T风格-源操作数在前，目的操作数在后。):


https://blog.csdn.net/weixin_44240981/article/details/121736598

要懂缓冲区溢出首先需要一些汇编的知识(以下均为X86汇编)，比如其实我们程序工作是通过堆栈，这就涉及了三个寄存器esp,ebp和eip。esp是栈顶指针，ebp是栈基指针，eip寄存器是CPU需要读取的指令的地址，（注意实际只有一个函数栈）可以理解为程序执行的那个地址。当然还有一些寄存器。
EAX 是"累加器"(accumulator), 它是很多加法乘法指令的缺省寄存器。
EBX 是"基地址"(base)寄存器, 在内存寻址时存放基地址。
ECX 是计数器(counter), 是重复(REP)前缀指令和LOOP指令的内定计数器。
EDX 则总是被用来放整数除法产生的余数。

首先要知道gdb中si,ni,n和s
n(next) C语言级别，不会进入函数内部
s(step) C语言级别，会进入函数内部
si(step in) 汇编语言级，会进入函数内部
ni(next in) 汇编语言级，不会进入函数内部


https://live.bilibili.com/22275404

Makefile选项CFLAGS,LDFLAGS,LIBS
CFLAGS 表示用于 C 编译器的选项，
CXXFLAGS 表示用于 C++ 编译器的选项。
这两个变量实际上涵盖了编译和汇编两个步骤。
CFLAGS： 指定头文件（.h文件）的路径，如：CFLAGS=-I/usr/include -I/path/include。同样地，安装一个包时会在安装路径下建立一个include目录，当安装过程中出现问题时，试着把以前安装的包的include目录加入到该变量中来。
LDFLAGS：gcc 等编译器会用到的一些优化参数，也可以在里面指定库文件的位置。用法：LDFLAGS=-L/usr/lib -L/path/to/your/lib。每安装一个包都几乎一定的会在安装目录里建立一个lib目录。如果明明安装了某个包，而安装另一个包时，它愣是说找不到，可以抒那个包的lib路径加入的LDFALGS中试一下。
LIBS：告诉链接器要链接哪些库文件，如LIBS = -lpthread -liconv
简单地说，LDFLAGS是告诉链接器从哪里寻找库文件，而LIBS是告诉链接器要链接哪些库文件。不过使用时链接阶段这两个参数都会加上，所以你即使将这两个的值互换，也没有问题。
有时候LDFLAGS指定-L虽然能让链接器找到库进行链接，但是运行时链接器却找不到这个库，如果要让软件运行时库文件的路径也得到扩展，那么我们需要增加这两个库给"-Wl,R"：
LDFLAGS = -L/var/xxx/lib -L/opt/mysql/lib -Wl,R/var/xxx/lib -Wl,R/opt/mysql/lib
如果在执行./configure以前设置环境变量export LDFLAGS="-L/var/xxx/lib -L/opt/mysql/lib -Wl,R/var/xxx/lib -Wl,R/opt/mysql/lib" ，注意设置环境变量等号两边不可以有空格，而且要加上引号（shell的用法）。那么执行configure以后，Makefile将会设置这个选项，链接时会有这个参数，编译出来的可执行程序的库文件搜索路径就得到扩展了。
 
 gcc编译器 CFLAGS 标志参数说明
CFLAGS = -g -O2  -Wall -Werror -Wno-unused
 编译出现警告性错误unused-but-set-variable，变量定义但没有使用，解决方法:
 增加 CFLAGS 或CPPFLAGS参数如下：
  CPPFLAGS=" -Werror -Wno-unused-but-set-variable" || exit 1

GCC总体选项列表
后 缀 名  所对应的语言
-S   只是编译不汇编，生成汇编代码
-E   只进行预编译，不做其他处理
-g   在可执行程序中包含标准调试信息
-o file 把输出文件输出到file里
-v  打印出编译器内部编译各过程的命令行信息和编译器的版本
-I dir 在头文件的搜索路径列表中添加dir目录
-L dir 在库文件的搜索路径列表中添加dir目录
-static 链接静态库
-llibrary 连接名为library的库文件
在include语句中，“<>”表示在标准路径中搜索头文件，““””表示在本目录中搜索。

 Gcc hello_sq.c –L /root/workplace/Gcc/lib –lsunq –o hello_sq
需要注意的是，“-I dir”和“-L dir”都只是指定了路径，而没有指定文件，因此不能在路径中包含文件名。
另外值得详细解释一下的是“-l”选项，它指示Gcc去连接库文件libsunq.so。由于在Linux下的库文件命名时有一个规定：必须以lib三个字母开头。因此在用-l选项指定链接的库文件名时可以省去lib三个字母。也就是说Gcc在对”-lsunq”进行处理时，会自动去链接名为libsunq.so的文件。

Gcc的告警和出错选项如表3.8所示。
选 项   含 义
-ansi 支持符合ANSI标准的C程序
-pedantic 允许发出ANSI C标准所列的全部警告信息
-pedantic-error 允许发出ANSI C标准所列的全部错误信息
-w  关闭所有告警
-Wall 允许发出Gcc提供的所有有用的报警信息
-werror 把所有的告警信息转化为错误信息，并在告警发生时终止编译过程

Gcc体系结构相关选项列表

选 项  含 义
-mcpu=type  针对不同的CPU使用相应的CPU指令。可选择的type有i386、i486、pentium及i686等
-mieee-fp 使用IEEE标准进行浮点数的比较
-mno-ieee-fp  不使用IEEE标准进行浮点数的比较
-msoft-float  输出包含浮点库调用的目标代码
-mshort 把int类型作为16位处理，相当于short int
-mrtd  强行将函数参数个数固定的函数用ret NUM返回，节省调用函数的一条指令

编译.c文件时用CFLAGS ， 编译.S文件时用AFLAGS

怎么编译文件就是意味着编译选项和连接选项是什么。
这些选项分成3类：全局的（适用整个代码树）、局部的（适用单个Makefile）、个体的（适用单个文件）。
全局选项是在顶层Makefile和arch/$(ARCH)/Makefile中定义的，这些选项是CFLAGS、AFLAGS、LDFLAGS、ARFLAGS，它们分别是编译C文件的选项，编译汇编文件的选项，连接文件的选项，制作库文件的选项。
局部选项在各自子目录中定义，名称为：EXTRA_CFLAGS、EXTRA_AFALGS、EXTRA_LDFALGS、EXTRA_ARFLAGS.
对单文件设定编译选项，可以用CLFAGS_$@、AFLAGS_$@，前者对C文件，后者对汇编文件。
注意：3类选项是一起使用的，在scripts/Makefile.lib中可以看到：
_c_flags = $(CFLAGS) $(EXTRA_CFLGAS) $(CFALGS_$(baseterget.o))



In file included from kpatch_storage.c:28:
include/kpatch_ptrace.h:82:13: error: 'struct user_regs_struct' declared inside parameter list will not be visible outside of this definition or declaration [-Werror]
   82 |      struct user_regs_struct *pregs);
      |             ^~~~~~~~~~~~~~~~
include/kpatch_ptrace.h:145:23: error: 'struct user_regs_struct' declared inside parameter list will not be visible outside of this definition or declaration [-Werror]
  145 | void copy_regs(struct user_regs_struct *dst,
      |                       ^~~~~~~~~~~~~~~~
include/kpatch_ptrace.h:152:14: error: 'struct user_regs_struct' declared inside parameter list will not be visible outside of this definition or declaration [-Werror]
  152 |       struct user_regs_struct *pregs,
      |              ^~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make: *** [Makefile:78: kpatch_storage.o] Error 1


https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/arch/riscv/lib?h=linux-5.19.y
https://lwn.net/Articles/873058/
https://lwn.net/Articles/365835/

The whole evolution of the credit markets resulted in all sorts of complex financial instruments that are difficult to unwind.
信贷市场的演进造就了多种多样难以解开的复杂金融工具。


RISC-V汇编和ARM汇编语法相同吗？
都是RISC架构，汇编自然挺像的。比如都是三操作数，load/store操作内存，都没有段寄存器这么一说（相比于x86）。但差别不只是寄存器的名字。有一个项目里面有这两个架构的汇编，Linux。在kernel里面会有汇编程序的，我对他们的些许了解就是从那里来的了。

作者：Reticence
链接：https://www.zhihu.com/question/431924608/answer/1594463658
来源：知乎
著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。


https://elixir.bootlin.com/linux/latest/source/arch/arm/kernel

https://vlsi.eelabs.technion.ac.il/projects/power-arm-to-risc-v-assembly-converter/

arm-linux-gcc、arm-none-eabi-gcc、arm-eabi-gcc、arm-none-linux-gnueabi-gcc区别
arm-none-eabi-gcc
==（ARM architecture，no vendor，not target an operating system，complies with the ARM EABI）==

Arm官方用于编译 ARM 架构的裸机系统（包括 ARM Linux 的 boot、kernel，==不适用编译 Linux应用==），一般适合 ARM7、Cortex-M 和 Cortex-R 内核的芯片使用，所以不支持那些跟操作系统关系密切的函数，比如fork(2)，他使用的是 newlib 这个专用于嵌入式系统的C库。

arm-none-linux-gnueabi-gcc
==(ARM architecture, no vendor, creates binaries that run on the Linux operating system, and uses the GNU EABI)==

主要用于基于ARM架构的Linux系统，==可用于编译 ARM 架构的 u-boot、Linux内核、Linux应用等==。arm-none-linux-gnueabi基于GCC，使用Glibc库，经过 Codesourcery 公司优化过推出的编译器。arm-none-linux-gnueabi-xxx 交叉编译工具的浮点运算非常优秀。一般ARM9、ARM11、Cortex-A 内核，带有 Linux 操作系统的会用到。

arm-eabi-gcc
Android ARM 编译器。

armcc
ARM 公司推出的编译工具，功能和 arm-none-eabi 类似，可以编译裸机程序（u-boot、kernel），但是不能编译 Linux 应用程序。armcc一般和ARM一起，Keil MDK、ADS、RVDS和DS-5中的编译器都是armcc，所以 armcc 编译器都是收费的）。

arm-none-uclinuxeabi-gcc 和 arm-none-symbianelf-gcc
arm-none-uclinuxeabi 用于uCLinux，使用Glibc。
arm-none-symbianelf 用于symbian。

ABI 和 EABI
ABI
二进制应用程序接口(Application Binary Interface (ABI) for the ARM Architecture)。在计算机中，应用二进制接口描述了应用程序（或者其他类型）和操作系统之间或其他应用程序的低级接口。

EABI
嵌入式ABI。嵌入式应用二进制接口指定了文件格式、数据类型、使用、堆积组织优化和在一个嵌入式软件中的参数的标准约定。开发者使用自己的汇编语言也可以使用 EABI 作为与兼容的生成的汇编语言的接口。

==两者主要区别是，ABI是计算机上的，EABI是嵌入式平台上（如ARM，MIPS等）。==

arm-linux-gnueabi-gcc 和 arm-linux-gnueabihf-gcc
两个交叉编译器分别适用于 armel 和 armhf 两个不同的架构，armel 和 armhf 这两种架构在对待浮点运算采取了不同的策略（有 fpu 的 arm 才能支持这两种浮点运算策略）。

其实这两个交叉编译器只不过是 gcc 的选项 -mfloat-abi 的默认值不同。gcc 的选项 -mfloat-abi 有三种值:

soft、softfp、hard（其中后两者都要求 arm 里有 fpu 浮点运算单元，soft 与后两者是兼容的，但 softfp 和 hard 两种模式互不兼容）：

soft： 不用fpu进行浮点计算，即使有fpu浮点运算单元也不用，而是使用软件模式。
softfp： armel架构（对应的编译器为 arm-linux-gnueabi-gcc ）采用的默认值，用fpu计算，但是传参数用普通寄存器传，这样中断的时候，只需要保存普通寄存器，中断负荷小，但是参数需要转换成浮点的再计算。
hard： armhf架构（对应的 arm-linux-gnueabihf-gcc ）采用的默认值，用fpu计算，传参数也用fpu中的浮点传，省去了转换，性能最好，但是中断负荷高。
------------------------------------------
https://github.com/schorrm/arm2riscv
Usage
Pipe Arm assembly in, it writes RISC-V assembly to stdout.
For instance, the test code is essentially along the lines of: armgcc -S -o - <input file name> | python3 arm2riscv.py | rvgcc -x assembler - -static.
For compiling C to RISC-V through the transpiler, there's a shortcut: ./compile_thru_arm2riscv.sh <file name>.
Run python3 arm2riscv.py -h for fuller usage and flags.
Test Suite
python3 test_suite.py runs the testing.
------------------------------------------
https://www.freebuf.com/vuls/275787.html
https://www.bilibili.com/video/BV1bq4y1J7ci      -------------englsih
priority 

https://bbs.elecfans.com/jishu_2290739_87_1.html#comment_top
https://www.itdaan.com/blog/2017/10/08/cd96f4d1de450af328068a3908621896.html


if(likely(foo))  //认为foo通常为1
if(unlikely(foo)) //认为foo通常为0 

1. 蜂鸟E200    https://github.com/SI-RISCV
2. pilpino    https://github.com/pulp-platform/pulpino
3. rocket   https://github.com/freechipsproject/rocket-chip
4. boom  https://github.com/riscv-boom/riscv-boom
5 SweRV  https://github.com/westerndigitalcorporation/swerv_eh1



(base) [root@host-12-0-0-19 libcareplus]# pwd
/data/libcare/libcareplus

make -C src install
make: Entering directory '/data/libcare/libcareplus/src'
install -d //usr/local/bin
install -m 0500 libcare-ctl //usr/local/bin/libcare-ctl
install -m 0500 libcare-server //usr/local/bin/libcare-server
install -m 0500 libcare-client //usr/local/bin/libcare-client
install -m 0500 libcare-cc //usr/local/bin/libcare-cc
install -m 0500 libcare-patch-make //usr/local/bin/libcare-patch-make
install -m 0500 kpatch_gensrc //usr/local/bin/kpatch_gensrc
install -m 0500 kpatch_make //usr/local/bin/kpatch_make
install -m 0500 kpatch_strip //usr/local/bin/kpatch_strip
install -m 0500 libcare-dump //usr/local/bin/libcare-dump
make: Leaving directory '/data/libcare/libcareplus/src'
(base) [root@host-12-0-0-19 libcareplus]# libcare-ctl -h
usage: libcare-ctl [options] <cmd> [args]

Options:
  -v          - verbose mode
  -h          - this message

Commands:
  patch  - apply patch to a user-space process
  unpatch- unapply patch from a user-space process
  info   - show info on applied patches

warning: implicit declaration of function ‘sleep’
#include <unistd.h>

https://akkadia.org/drepper/
https://akkadia.org/drepper/tls.pdf
https://pubs.opengroup.org/onlinepubs/009695399/
https://pubs.opengroup.org/onlinepubs/9699919799/functions/contents.html
http://bxr.su/OpenBSD/sys/sys/tree.h

https://ftp.lug.ustc.edu.cn/


gpr  general-purpose registers (GPR's) 

XLEN的值一般跟RISC-V CPU架构有关系。 如果是32位架构的CPU，那么XLEN的值就是32。图1中XLEN-1 = 32-1 =31，即在一个通用寄存器中的最高位为31。在64位CPU中通用寄存器的宽度是64，同时PC宽度也是64位，最高位为64-1 =63。


riscv64-unknown-elf-gcc -o rv-hello rv-hello.s -nostdlib -static
riscv64-unknown-elf-run rv-hello

ADDI rd, zero, immediate, to load a value (immediate) into register rd.
LW rd, offset(rs1) to load content at memory location offset + rs1 into register rd.
SW rd, offset(rs1) store content of rd register at memory location offset + rs1.
ADD rd, rs1, rs2 to content of two registers and store value in destination register rd.
The branch instructions BEQ, BNE, BLT. Read previous story for more details.


https://cloud.binary.ninja/bn/f3686e2c-2d11-48a0-8a57-9287201143cb?view=Disassembly&function=4198400&address=4198400


ADDI RD, RS, imm
Where RD is the destination register, RS the source register and imm is a 12-bit immediate value. Instructions are 32-bits in length so the size of the immediate value tends to be whatever is leftover after setting the opcode and any required registers.
You can define a NOP instruction with:
      ADDI x0, x0, 0
Or load immediate with:

      ADDI RD, X0, imm
The Assembler will take opcodes like NOP or LI (Load Immediate) and translate them into the correct underlying instruction. Here we used ADDI, but when we decompile the compiled program we’ll see the decompiler uses these aliases. These do make your program more readable. All our ADDI instructions use the LI pattern.

Risc-V provides a separate opcode to call the operating system. This is the ECALL instruction. When calling Linux, A7 is the Linux service number and A0 to A6 contain any parameters. When calling write, we need the file descriptor (1 for stdout), the string to write and the length in bytes to write, which we put in registers A0, A1 and A2. The return code which we don’t check will be in A0. This differs from most other architectures that use the interrupt mechanism for this purpose. The Risc-V designers feel it is cleaner to separate operating system calls from interrupts, even though both cause kernel privileged instructions to execute.

The remaining instruction is LA, which isn’t a Risc-V instruction, but rather it tells the Assembler that we want to load an address into a register. Then we leave it up to the Assembler to figure out how to do this. If we are running with 64-bit addressing then this address will be 64-bits. We can’t load this with a single load immediate instruction since the biggest immediate value is 20-bits, with most smaller. This means to load the address we either need to do many instructions to load this address piece by piece using load immediates, shifts, logical operations and/or arithmetic operations. The Assembler has inside knowledge of the value of this address, so it can, say use PC relative addressing to load this address. There are a lot of tricks to deal with 64-bit values from 32-bit instructions, that we don’t have room to go into now, but perhaps in a future blog article.
https://bellard.org/tinyemu/buildroot.html
https://bellard.org/tinyemu/


This article describes the calling conventions used when programming x86 architecture microprocessors.

Calling conventions describe the interface of called code:

The order in which atomic (scalar) parameters, or individual parts of a complex parameter, are allocated
How parameters are passed (pushed on the stack, placed in registers, or a mix of both)
Which registers the called function must preserve for the caller (also known as: callee-saved registers or non-volatile registers)
How the task of preparing the stack for, and restoring after, a function call is divided between the caller and the callee

This is intimately related with the assignment of sizes and formats to programming-language types. Another closely related topic is name mangling, which determines how symbol names in the code are mapped to symbol names used by the linker. Calling conventions, type representations, and name mangling are all part of what is known as an application binary interface (ABI).

https://github.com/dynup/kpatch/issues/1296
PC 加立即数 (AddUpperImmediate toPC)  ---auipc

auipc rd, immediate    //x[rd] = pc + sext(immediate[31:12] << 12)PC 加立即数 (AddUpperImmediate toPC).U-type, RV32I and RV64I.把符号位扩展的 20 位（左移 12 位）立即数加到 pc 上，结果写入 x[rd]。
jalr rd, offset(rs1)    // t =pc+4; pc=(x[rs1]+sext(offset))&~1; x[rd]=t跳转并寄存器链接 (JumpandLinkRegister).I-type, RV32I and RV64I.把 pc 设置为 x[rs1] + sign-extend(offset)，把计算出的地址的最低有效位设为 0，并将原 pc+4的值写入 f[rd]。 rd 默认为 x1。压缩形式： c.jr rs1; c.jalr rs1

sext其实是sign-extend（符号扩展）的缩写表示将该立即数扩展到32位，如果是有符号数则进行符号扩展，若是无符号数则进行无符号扩展
lui (Load Upper Immediate): this sets rd to a 32-bit value with the low 12 bits being 0 and the high 20 bits coming from the U-type immediate.auipc (Add Upper Immediate to Program Counter): this sets rd to the sum of the current PC and a 32-bit value with the low 12 bits as 0 and the high 20 bits coming from the U-type immediate.
auipc (Add Upper Immediate to Program Counter): this sets rd to the sum of the current PC and a 32-bit value with the low 12 bits as 0 and the high 20 bits coming from the U-type immediate.
RISC-V架构有两条无条件跳转指令（Unconditional Jump），即jal指令与jalr指令。跳转链接（Jump and Link）指令——jal指令可用于进行子程序调用，同时将子程序返回地址存在链接寄存器（Link Register，由某一个通用整数寄存器担任）中。跳转链接寄存器（Jump and Link-Register）指令——jalr指令能够用于子程序返回指令，通过将jal指令（跳转进入子程序）保存的链接寄存器用于jalr指令的基地址寄存器，则可以从子程序返回
jal使用立即（20位）编码作为目标地址，并且可以跳转+ -1MiB范围。并将实际的address + 4保存在寄存器rd中。 （在您的示例中为x1 ）。jalr使用间接地址（在您的示例中为x1）加上12位常量（在您的示例中为0）。它将实际的address + 4也保存在寄存器rd中。在您的示例中，您将x0设置为返回地址寄存器是因为您“不在乎”。例如，当您从子例程返回时，返回地址没有用，则我们设置x0
jalr使用间接地址（在您的示例中为x1）加上12位常量（在您的示例中为0）。它将实际的address + 4也保存在寄存器rd中。在您的示例中，您将x0设置为返回地址寄存器是因为您“不在乎”。例如，当您从子例程返回时，返回地址没有用，则我们设置x0


the first operand is implicitly used as a scratch register. 
the last operand specifies the scratch register to be used.  

ra is implicitly used to save the return address. 
similar to call <symbol>, but <rd> is used to save the return address instead. 
t1 is implicitly used as a scratch register. 
similar to tail <symbol>, but <rt> is used as the scratch register instead. 


作者：裴喜龙
链接：https://zhuanlan.zhihu.com/p/354654279
来源：知乎
著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。

https://github.com/riscv-non-isa/riscv-asm-manual/blob/master/riscv-asm.md


https://eseo-tech.github.io/emulsiV/
https://venus.cs61c.org/
https://www.cs.cornell.edu/courses/cs3410/2019sp/riscv/interpreter/#

JAL rd, offset    # rd ← PC + 4, PC ← PC + offset
JALR rd, offset(rs1)    # rd ← PC + 4, PC ← rs1 + offset
AUIPC rd, immediate    # rd ← PC + immediate[31:12] << 12

https://en.cppreference.com/w/cpp/utility/bitset
https://venus.kvakil.me/
https://medium.com/swlh/risc-v-assembly-for-beginners-387c6cd02c49
